JSP stands for Java Server Pages. JSP is a server side web technology like Servlet. JSP is extension of Servlet not enhancement of Servlet. It does not replace the servlet technology and can be used with Servlets. JSP is used to develop server side web components to generate and render dynamic contents
Such design (Java with in HTML) allows smooth development of presentation layer. A UI designer/developer can develop HTML pages that are rich in UI feature. A Java developer can later easily embed JSP within these HTML pages. In case of servlets all the HTML contents should be embedded in Java. To achieve rich UI feature, servlets become more of HTML than java. Any change in UI, enforces change in Java code. Change in Java code needs compilation of Java file. This becomes more expensive
The typical request-response flow is depicted for JSP
LoginServlet.java
as per the URL pattern configurationIf welcome.jsp was not there then LoginServlet.java has to perform other tasks also like fetching the data, preparing HTML response, setting this data in HTML. All UI stuff servlet has to do means by Java Developer but most likely UI developer are more efficient to do UI stuff. So in case of servlet either Java developer should learn UI technologies or always work closely with any UI developer.
On the other hand in case of JSP this task of Java and HTML can be separated. Let Java developer write LoginServlet.java. Let UI developer write welcome.jsp in HTML only. Later let Java Developer embed the JSP code in welcome.jsp
There are mainly three JSP lifecycle method:
_jspInit()
_jspService(HttpServletRequest req, HttpServletResponse res)
_jspDestroy()
_jspinti()
for initialization_jspService()
_jspService()
_jspDestroy()
<%
...
%>
<%
int i = 10;
out.println(i);
System.out.println(i);
...
%>
_jspService(){
...
int i = 10;
out.println(i);
System.out.println(i);
...
}
out.print(exp)
and then in _jspService()
<%=
exp
%>
<% String name = “Atul”; %>
<%= “Name” %>
<%= name %>
_jspService(){
...
String name = “Atul”;
out.print(“Name”);
out.print(name);
...
}
_jspService()
method<%!
//global var
//methods
}
%>
<%!
String name = “Atul”;
void myMethod(){
...
}
%>
<h1>My Heading</h1>
public class welcome_jsp extends HttpServlet {
String name = “Atul”;
void myMethod(){
...
}
public void init(ServletConfig config) throws ServletException {}
public void destroy(){}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PageContext pageContext = null;
HttpSession session = null
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = null;
...
out.write(“<h1>My Heading</h1>”);
...
int i = 10;
out.print(i);
System.out.println(i);
...
}
}
Directive is a special instructions to container to perform the required task. There are mainly three type of directives
<%@ include file=”filename1.jsp” %>
<%@ include file=”filename2.html” %>
page directive is used to perform page level operations eg. import, thread safe
<%@ page
language=””
import=””
session=””
extends=””
isThreadSafe=””
errorPage=””
isErrorPage=””
isELIgnored=””
%>
<%@ page language=”java” %>
<%@ page import=”java.io.*,java.util.*””%>
<%@ page session=”false” %>
<%@ page extends=”javax.servlet.http.HttpServlet” %>
<%@ page import=”java.io.IOException,javax.servlet.ServletException”%>
<%@ page extends=”javax.servlet.http.HttpServlet” %>
<html>
<body>
<h1>
<%!
public void service(HttpServletRequest req, HttpServletresponse) throws IOException, ServletException{
_jspService(req, res);
}
%>
</h1>
</body>
</html>
<%@ page isThreadSafe=”false” %> //Servlet Single Thread Model
<%@ page isThreadSafe=”true” %> //Servlet Multi Thread Model
index.jsp
<%@ page errorPage=”error.jsp” %>
<html>
<body>
<h1>
Welcome to Home
</h1>
<% int i = 10/0; %>
</body>
</html>
error.jsp
<%@ page isErrorPage=”true” %>
<html>
<body>
<h1>Error Occurred!</h1>
<%= exception %>
</body>
</html>
index.jsp
<%@ page isELIgnored=”false” %>
<html>
<body>
<% request.setAttribute(“msg”, “My message”); %>
<h1>Messae: ${msg}</h1>
</body>
</html>
taglib directive is used to use the custom tags in JSP
<jsp:include>
<jsp:forward>
<jsp:param>
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:include>
functionality is same as of RequestDispatcher.include
method<jsp:include page=”JSP or HTML file with extension” />
<jsp:forward>
functionality is same as of RequestDispatcher.forward
method<jsp:forward page=”JSP or HTML file with extension” />
<jsp:include>
or <jsp:forward>
<jsp:param name=”mobile” value=”9876543210” />
<jsp:useBean>
is used for creating Java Bean object<jsp:useBean id=”” class=”” scope=”” />
<jsp:useBean id=”user” class=”com.learnjsp.bean.User” scope=”session” />
<jsp:setProperty>
is used for storing data in Java Bean object<jsp:setProperty name=”” property=”” value=”” />
<jsp:setProperty name=”user” property=”userName” value=”Atul” />
<jsp:getProperty>
is used to fetch data from Java Bean object<jsp:getProperty name=”” property=”” />
<jsp:getProperty name=”user” property=”userName” />
void setAttribute(String attrName, Object attrValue)
Object getAttribute(String attrName)
void removeAttribute(String attrName)
Enumeration getAttributeNames()
There are mainly three types of servlet scopes
HttpServletRequest
will be available until request object destroyedHttpSession
object will be available until session persistsServletContext
will be available throughout the life of web applicationThere are four types of JSP scopes
setAttribute
butpageContext
java.lang.Object
typePageContext
to manage attributes and its valuesvoid setAttribute(String attrName, Object attrValue)
void setAttribute(String attrName, Object attrValue, int scope)
Object getAttribute(String attrName)
Object getAttribute(String attrName, int scope)
Object findAttribute(String attrName)
void removeAttribute(String attrName, int scope)
Enumeration getAttributeNamesInScope(int scope)
HttpServletRequest
will be available until request object destroyedHttpSession
object will be available until session persistsServletContext
will be available throughout the life of web application