JSP Complete
JSP Complete
(JSP)
Java Server Pages (JSP)
▪ Static content
– HTML, XML, Text
▪ Dynamic content
– Java code
– Displaying properties of JavaBeans
– Invoking business logic defined in Custom tags
Advantages of JSP over Servlet?
▪ 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.
▪ 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.
Advantages of JSP over Servlet?
▪ Comments tag
▪ Declaration tag
▪ Expression tag
▪ Scriptlet tag
▪ Directive Tag
JSP Scripting Elements – Comments
<html> <body>
A Test of Comments
<%-- comment not be visible in the page --%>
</body> </html>
JSP Scripting Elements – Declaration
<html>
<body>
<%= request.getParameter("uname") %>
</body>
</html>
JSP Scripting Elements – Scriptlet
▪ Scriptlet tag: is used to execute java source code in JSP.
Scriptlet tag can only declare variables not methods.
The declaration of scriptlet code is placed inside
_jspService() method. So, it get memory at each
request.
<html> <body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</body>
</html>
JSP Directives
▪ The jsp directives are messages that tells the web container
how to translate a JSP page into the corresponding servlet.
▪ Example:
<%@ page import = "java.util.Date" %>
<html>
<body>
Today is: <%= new Date() %>
</body>
</html>
JSP Page Directive – contentType
▪ Example:
<%@ page contentType = “application/msword” %>
OR
<%@ page contentType = “text/html” %>
<html>
<body> My Page </body>
</html>
JSP Page Directive – info
<html>
<body> My Page </body>
</html>
JSP Page Directive – language
<html>
<body> My Page </body>
</html>
JSP Page Directive – isELIgnored
<html>
<body>
EL expression result is: ${2 * 4 + 3 * 4}
</body>
</html>
JSP Page Directive – 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 default 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:
<html>
<body> My Page </body>
</html>
JSP Page Directive – errorPage
<html>
<body> Sorry an Exception Occurred
The exception is: <%= exception %>
</body>
</html>
JSP Page buffer and autoflush
<html>
<body> My Page </body>
</html>
JSP Include Action
▪ To write data for the jsp, we need to use the JspWriter class.
▪ The PageContext class provides getOut() method that returns
the instance of JspWriter class.
package myJSP.com;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
/*This is just to display a message, when we will use our custom tag.
This message would be displayed */
JspWriter out = getJspContext().getOut();
out.println("Hello Custom Tag!");
}
}
Create the Tag Library Descriptor (TLD)
▪ Tag Library Descriptor (TLD) file contains information of tag and Tag
Hander classes.
▪ It must be contained inside the WEB-INF directory.
▪ Should have a .tld extension.
<uri>/WEB-INF/custom</uri>
<tag>
<name>MyMsg</name>
<tag-class>com.HelloTag</tag-class>
</tag>
</taglib>
Using custom tag in JSP
▪ taglib directive should have the TLD file path in uri field.
▪ We have created message.tld file so we have given the path
of that file.
<html>
<head>
<title>Custom Tags in JSP Example</title>
</head>
<body>
<myprefix:MyMsg/>
</body>
</html>
JSP Implicit Objects
▪ There are 9 jsp implicit objects. These objects are created by the
web container that are available to all the JSP pages.
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
JSP Implicit Object – out
<html> <body>
<% out.print(“Welcome JSP”); %>
</body>
</html>
JSP Implicit Object – request
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
=================================
<%
String name = request.getParameter("uname");
out.print("welcome "+name);
%>
JSP Implicit Object – response
▪ <form action="welcome.jsp">
▪ <input type="text" name="uname">
▪ <input type="submit" value="go"><br/>
▪ </form>
<%
response.sendRedirect("http://www.google.com");
%>
JSP Implicit Object – config
▪ The jsp include action tag includes the resource at request time so
it is better for dynamic pages because there might be changes in
future.