java_unit6.docx
java_unit6.docx
Java Server Pages: Introducing Java Server Pages, JSP Overview, Setting Up the JSP Environment,
Generating Dynamic Content, Using Custom Tag Libraries and the JSP Standard Tag Library,
Processing Input and Output.
JSP
JSP technology is used to create web application just like Servlet technology. It can be
thought of as an extension to servlet because it provides more functionality than servlet
such as expression language, jstl etc.
A JSP page consists of HTML tags and JSP tags. The jsp pages are easier to maintain than
servlet because we can separate designing and development. It provides some additional
features such as Expression Language, Custom Tag etc.
1) Extension to Servlet
JSP technology is the extension to servlet technology. We can use all the features of servlet
in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with
presentation logic. In servlet technology, we mix our business logic with the presentation
logic.
Note: jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
As depicted in the above diagram, JSP page is translated into servlet by the help of JSP
translator. The JSP translator is a part of webserver that is responsible to translate the JSP
page into servlet. Afterthat Servlet page is compiled by the compiler and gets converted into
the class file. Moreover, all the processes that happens in servlet is performed on JSP later
like initialization, committing response to the browser and destroy.
index.jsp
Let's see the simple example of JSP, here we are using the scriptlet tag to put java code in
the JSP page. We will learn scriptlet tag later.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
o scriptlet tag
o expression tag
o declaration tag
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
JSP expression tag
The code placed within JSP expression tag is written to the output stream of the response.
So you need not write out.print() to write data. It is mainly used to print the values of
variable or method.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of expression tag.
index.jsp
<html>
<body>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>
The code written inside the jsp declaration tag is placed outside the service() method of auto
generated servlet.
index.jsp
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
The available implicit objects are out, request, config, session, application etc.
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
It can also be used to set, get and remove attributes from the jsp request scope.
Let's see the simple example of request implicit object where we are printing the name of
the user with welcome message.
Example of JSP request implicit object
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
1. PrintWriter out=response.getWriter();
index.jsp
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
JSP directives
The jsp directives are messages that tells the web container how to translate a JSP page
into the corresponding servlet.
o page directive
o include directive
o taglib directive
import
contentType
extends
info
buffer
language
isELIgnored
isThreadSafe
autoFlush
session
pageEncoding
errorPage
isErrorPage
1)import
The import attribute is used to import class,interface or all the members of a package.It is
similar to import keyword in java class or interface.
2)contentType
The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of
the HTTP response.The default value is "text/html;charset=ISO-8859-1".
3)extends
The extends attribute defines the parent class that will be inherited by the generated
servlet.It is rarely used.
4)info
This attribute simply sets the information of the JSP page which is retrieved later by using
getServletInfo() method of Servlet interface.
</body>
</html>
The web container will create a method getServletInfo() in the resulting servlet.For example:
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP
page.The default size of the buffer is 8Kb.
</body>
</html>
6)language
The language attribute specifies the scripting language used in the JSP page. The default
value is "java".
7)isELIgnored
We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default
its value is false i.e. Expression Language is enabled by default. We see Expression
Language later.
1. <%@ page isELIgnored="true" %>//Now EL will be ignored
8)isThreadSafe
Servlet and JSP both are multithreaded.If you want to control this behaviour of JSP page,
you can use isThreadSafe attribute of page directive.The value of isThreadSafe value is
true.If you make it false, the web container will serialize the multiple requests, i.e. it will
wait until the JSP finishes responding to a request before passing another request to it.If
you make the value of isThreadSafe attribute like:
The web container in such a case, will generate the servlet as:
9)errorPage
The errorPage attribute is used to define the error page, if exception occurs in the current
page, it will be redirected to the error page.
</body>
</html>
<html>
<body>
<%@ include file="header.html" %>