Module 2
Module 2
Introduction to JSP
There are three main types of JSP constructs that you embed in a page:
o scripting elements
o directives
o actions
2.1.1 lifecycle of JSP
JSP pages are saved with .jsp extension which lets the server know that this is a JSP page and
needs to go through JSP life cycle stages.
Scripting elements
Comments
Directives
Actions
Scripting elements
Example:
1. <html>
<body>
<% out.print("welcome to jsp"); %>
2. </body>
3. </html>
Example
<html>
<body>
<%! int data=50 %>
</body>
</html>
General form:
Example:
Directives
o JSP directives let you give directions to the server on how a page should be
processed. There are three directives in JSP.
Directive Description
<%@page...%> defines page dependent properties such as
language, session, errorPage etc.
<%@ include…%> defines file to be included.
<%@ taglib…%> declares tag library used in the page
Actions
o The action tags are used to control the flow between pages and to use Java
Bean. The Jsp action tags are given below.
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.
jsp:param sets the parameter value. It is used in forward and include
mostly.
jsp:fallback can be used to print the message if plugin is working. It is
used in jsp:plugin.
Extra questions
GET POST
HTTP The request contains only Along with request line and header it also
Request the request line and HTTP contains HTTP body.
header
Parameter The form elements are The form elements are passed in the body of
Passing passed to the server by the HTTP request.
appending at the end of the
URL
Size The parameter data is Can send huge amount of data to the server.
limited(the limit depends
on the container)
Usage Generally used to fetch Generally used to process the sent data
some information from the
host.
sendRedirect
o In case of sendRedirect() method, the request is transferred to another resource
to a different domain or the different server for further processing
o When developers use the sendRedirect(), the web-container transfers the
request to a client or a browser so that the URL given inside
the sendRedirect() method is visible as a new request to the client
o In case of sendRedirect() call, the old request and response object is lost because
it is treated as a new request by the browser
o In browser’s address bar, developers are able to see the new redirected address
i.e. it is not transparent
o sendRedirect() is slower as one extra round trip is required i.e. The complete
new request is created and the old request object is lost
o In case of sendRedirect() call, if developers want to store the data they will do
it in a Session object or pass it along the value with the URL
sendError
o The javax.servlet.http.HttpServletResponse class has two
versions of the sendError( ) method:
one that takes an int parameter representing the HTTP response
code (such as 500), and the other taking an int parameter and
a String error message.
The String parameter is used to display a message to the client if an
error page is not configured for that particular response code.
JSP Scripting element are written inside <% %> tags. These code inside <% %> tags
are processed by the JSP engine during translation of the JSP page. Any other text in
the JSP page is considered as HTML code or plain text.
1. Expressions of the form, which are evaluated and inserted into the servlet’s
output.
2. Scriptlets of the form, which are inserted into the servlet’s _jspService
method (called by service).
3. Declarations of the form, which are inserted into the body of the servlet class,
outside any existing methods.
4. Directive is used to provide instructions to the container.
<!DOCTYPE html>
<html>
<head><title>JSP Page</title></head>
<body>
<h1>JSP Expression</h1>
<UL>
<li>Current time: <%= new java.util.Date() %></li>
<li>Server: <%= application.getServerInfo() %></li>
<li>Session ID: <%= session.getId() %></li>
<li> Expression Evaluation: <%= 2 * 6% ></li>
</UL>
</body>
</html>
JSP Servlet
JSP is a web page scripting language that can Servlets are already compiled which also
generate dynamic content creates dynamic web content
JSP runs slower compared to servlet as it takes Servlets run faster compared to JSP.
compilation time to convert into java Servlets
It’s easier to code in JSP than in Java Its little much code to write here.
Servlets.
The advantage of JSP programming over There is no such custom tag facility in
servlets is that we can build custom tags which servlets.
can directly call Java beans.
We can achieve functionality of JSP at client There are no such methods for servlets.
side by running JavaScript at client side.
"</UL>\n" +
"</BODY></HTML>"); } }
2.11Writing scriptlets.
Scriptlet Tag allows to write java code inside JSP page
Scriptlet tag implements the _jspService method functionality by writing
script/java code.
Scriptlets have the following form:
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
---------------------------------------------------------
Or combination of a scriptlet and a JSP expression, as below
<% String queryData = request.getQueryString(); %>
Attached GET data: <%= queryData %>
---------------------------------------------------------
Or single JSP expression, as here
Attached GET data: <%= request.getQueryString() %>
<html>
<head><title>Even number program in JSP</title></head>
<body>
<%
for(int i=0;i<=10;i++)
{
if((i%2)==0)
{
out.print("Even number :"+i);
out.print("<br>");
}
}
out.println(“<br><br><br>First 10 odd numbers are”);
for(i=0;i<=10;i++)
{
if(i%2!=0)
{
out.println(i);
}
}
%>
</body>
</html>
<body>
<% if (Math.random() < 0.5) { %>
<H1>Have a <I>nice</I> day!</H1>
<% } else { %>
<H1>Have a <I>lousy</I> day!</H1>
<% } %>
</body>
You probably find the bold part a bit confusing, Simply follow the rules for how JSP
code gets converted to servlet code. Once you think about how this example will be
converted to servlet code by the JSP engine, you get the following easily understandable
result.
Overuse of this approach can lead to JSP code that is hard to understand and maintain.
Avoid using it to conditionalize large sections of HTML, and try to keep your JSP pages
as focused on presentation (HTML output) tasks as possible.
Since declarations do not generate output, they are normally used in conjunction with
JSP expressions or scriptlets.
In principle, JSP declarations can contain field (instance variable) definitions, method
definitions, inner class definitions, or even static initializer blocks: anything that is legal
to put inside a class definition but outside any existing methods.
In practice, however, declarations almost always contain field or method definitions.
Example:
<H1>Some Heading</H1>
<%!
private String randomHeading() {
return("<H2>" + Math.random() + "</H2>");
}
%>
<%= randomHeading() %>