Вы находитесь на странице: 1из 5

Java Assignment -2

1. Difference between JSP and Servlets.

Basis of Differentiation Servlet JSP


Java/ HTML code It is a Java-based code and It is an HTML based code
uses the java approach for and comprises of a tag-
functioning. based approach. JSP is an
interface that rests on top
of Servlets. JSPs are
extensions of servlets for
minimizing the efforts of
developers with regards to
writing user interfaces with
the help of Java
programming.
Levels of difficulty Writing servlet codes is JSP is comparatively less
more complicated than difficult to code as it
coding JSP as it comprises comprises of java in HTML.
of HTML in java.
Controller in MVC Servlets play the role of a JSP is typically a view in
controller in the MVC the MVC approach to show
approach. output.
Speed Servlets are faster than JSP takes longer than
JSP. servlet as the first step in
JSP’s lifecycle comprises
of the translation of JSP to
easy-to-decipher java code,
and then the compilation
takes place.
Requests accepted Servlets accept all types of JSP accepts only http
protocol requests. requests.
Overriding of service() In Servlets, it is possible to In JSP, it is not possible to
method replace the service() override the service()
method. method.
Enabling of session In the servlet, session Session management is
management management is not enabled enabled automatically in
by default. JS users are case of JSP
responsible for allowing
the same explicitly.
Javascript on the client- In Servlet, no methods Client-side validations can
side exist for running be used in JSP for running
JavaScript on the client- JavaScript on the client-
side. side server.
Life cycle methods Servlet has life cycle JSP presents life cycle
methods of the likes of methods like jspInit(),
init(), service() and _jspService() and
destroy(). jspDestroy().

2. Advantages of JSP over other competing technologies.

Advantages of JSP

Following table lists out the other advantages of using JSP over other technologies −

vs. Active Server Pages (ASP)

The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic
or other MS specific language, so it is more powerful and easier to use. Second, it is portable to
other operating systems and non-Microsoft Web servers.

vs. Pure Servlets

It is more convenient to write (and to modify!) regular HTML than to have plenty of println
statements that generate the HTML.

vs. Server-Side Includes (SSI)

SSI is really only intended for simple inclusions, not for "real" programs that use form data,
make database connections, and the like.

vs. JavaScript

JavaScript can generate HTML dynamically on the client but can hardly interact with the web
server to perform complex tasks like database access and image processing etc.

vs. Static HTML

Regular HTML, of course, cannot contain dynamic information

3. Explain the types of JSP scripting elements in detail.

A JSP page contains tags. The tags are in the form of scripting tags, directive tags and
comments. Tags help programmers to develop Java codeless JSP program. Implicit
objects increase the functionality of JSP page. Scripting element also helps in generating
dynamic content and displaying dynamic content.
Categorization of Tags
We have categorized the tags into mainly three types:
• Scripting tags
• Directive tags
• Standard action tags

JSP also has some commenting tags for Java code as well as for html code that are given in this
chapter. We will discuss the directive tags and action tags in the chapters.
JSP scripting elements enable us to insert code into the Servlet that is generated from JSP page.
In most of the cases, java is used as scripting language to build JSP page however other
supported languages can be used.
Types of Scripting Tags
There are three type of scripting tags:
1. Scriptlet tag
2. Declaration tag
3. Expression tag
1. Scriptlet Tag
Java code placed in scriptlet goes to _jspService (-,-) of JSP equivalent Servlet. We generally
place business logic or request processing logic in scriptlets. Then what happens for methods,
variables, and for implicit objects.
For methods
We can call methods from scriptlet but we cannot define methods in side scriptlet. Because our
defined method is going to be placed into _jspService (-,-) as a nested method but Java does not
support nested methods.
For variables
Scriptlet variables are locale variables in _jspService (-,-).
For implicit objects
All implicit objects are visible inside scriptlet
There are two syntaxes for JSP document/page.
•Standard syntax or traditional syntax
<% script codes %>
• XML based syntax
<jsp:scriptlet>
Script code
</jsp:script code>
Working with scriptlet
Create a deployment directory structure and place the JSP file parallel to WEB-INF and give
request to the JSP file as done in the above program.
a.jsp
<% int a=12;
int b=230;
int c=a*b;
out.println (“multiplication is” +c);
%>
In this example, JSP page prints multiplication of two number as output on the browser window.
2. Declaration Tag
Code placed in declaration tag goes outside of _jspService (-,-) in JSP equivalent Servlet. This
tag allows to declare instance and class variables and implements class methods, such as jspInit
(), jspDestroy (). Here we can
define methods. Implicit objects are not visible to declaration tag.
•Standard syntax of declaration tag
<%! Script codes %>
• Xml based syntax
<jsp:declaration>
Script codes
<jsp:declaration>
Example to use declaration tag
abc.jsp
<%! public int diff (int x, int y)    
{
   return x-y;
}%>
<% out.println (“difference is” +diff (87348,20));%>
3. Expression Tag
We have decided to make Java code less JSP program. Then why are we using out.println (). Is
there any option to avoid it! Yes by using expression tag?
This tag is used to evaluate expression and to write the generated output on browser window.
The code of this tag goes to _jspService (-,-) method of java equivalent Servlet. So in expression
tag? implicit objects are visible. This tag is, used for method calls, for displaying variables and to
evaluate the expression.
• Standard syntax of expression tag
<%= java expression/script code %>
• XML based syntax
<jsp:expression>
Java expression/script code
</jsp:expression>
Example using expression tag in JSP page
xyz.jsp
<% int a=70;
int b=20; %>
<font color=“red”>You have entered a value=: <%=a%> </font> <br>
<font color=“red”>You have entered b value=: <%=b%> </font> <br>
<font color= “green”> <b> Sum, difference, multiplication is respectively---->:
<%= (a+b) + “,”+ (a-b) + “ ”+ (a*b) %> </b> </font>
To execute multiple expression at a time, write: <%= (a+b)+ “”+(b+a) %>
The code
<%=a%> becomes
out.print (a);
If we place ‘;’ at the end of expression tag then
<%=a;%> it becomes
out.print(a;); -> this is not going to compile
So never end an expression tag with semicolon ( ;)
Point to be considered while using expression tags
Do not end an expression with ‘,’ because the expression given in this tag is
placed into the out.print () method. i.e. <%= i;%> becomes out.println (i;). The expression given
in the expression tag should not be void type.

Вам также может понравиться