Professional Documents
Culture Documents
JSP PDF
JSP PDF
Servlets JSP
1. Process request of the end user ( 1. Presents data to the end
business logic layer) user(Presentation layer)
2. Any changes in servlet code required to 2. Any changes in jsp code handled by
recomple and re deploy web container no need of re complilation
and re-deployment
3. It requires .class files and web.xml 3. .class files and web.xml are not required
4. Slow process 4. Faster than servlets
5. HTML code inside Java code(java with 5. Java code inside HTML(html with java)
html)
1. The user goes to a JSP page (ending with .jsp). The web browser makes the
request via the Internet.
3. The Web server recognizes that the file required is special (.jsp), therefore passes
the JSP file to the JSP Servlet Engine.
4. If the JSP file has been called the first time, the JSP file is parsed, otherwise go to
step 7.
5. The next step is to generate a special Servlet from the JSP file. All the HTML
required is converted to println statements.
6. The Servlet source code is compiled into a class.
7. The Servlet is instantiated, calling the init and service methods.
8. HTML from the Servlet output is sent via the Internet.
9. HTML results are displayed on the user’s web browser.
• there are three methods automatically called when a JSP is requested and
terminated.
• 1. jspInt()
• 2. jspDestroy()
• 3. _jspService().
• jspInt() and jspDestroy() both are same as init() and destroy() methods in
servlets.
• Service() method is automatically called and retrieves a connection to
HTTP.
1. JSPs are essential an HTML page with special JSP tags embedded.
These JSP tags can contain Java code.
2. The JSP file extension is .jsp rather than .htm or .html. The JSP engine
parses the .jsp and creates a Java servlet source file.
4. This is done the first time and this why the JSP is probably slower the
first time it is accessed. Any time after this the special compiled
servlet is executed and is therefore returns faster.
JSP Scripting elements
• Java server pages are written by embedding java code inside html.
• Scripting elements are used to write java code inside the html.
• JSP Scripting elements 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.
• Five different types of Scripting elements
Scripting elements Symbols
comments <%-- --%>
directive <%@ %>
declaration <%! %>
scriptlet <% %>
expression <%= %>
i). import: Import all the classes in a java package into the current JSP
page. This allows the JSP page to use other java classes.
• Tells the container that merge the external files like text file, jsp file or html
in the current jsp page.
//welcome.jsp
<html> <head>
<title>Welcome Page</title>
</head>
<body>
<%@ include file="header.jsp" %>
Welcome, User //header.jsp
</body> </html> <html>
<body>
<img src="header.jpg" alt="This is Header image" / >
</body> </html>
Taglib directive
• Tag library is set of user defined tags.
• JSP API allows you to define custom tags they look like html or xml tags.
• Taglib declares that your JSP page uses a set of custom tags, identifies the
location of the custom tags.
• <%@ taglib uri="uri" prefix="prefixOfTag" >
- uri attribute specifies the location of the custom tag.
- prefix- markup or short name.
Example on taglib directive
• In this example we are using the tag currentDate. To use this tag specify the
taglib so the container get information about this tag.
Standard actions
• Standard actions are well known tags that effect runtime behaviour of the
jsp and the response sent back to the client
• Predefined tags in jsp
Action tag Meaning
<jsp:useBean> Instantiate a JavaBean or locates existing
bean instance.
<jsp:useBean id="beanId" />
<jsp:getProperty> Get properties of java bean
<jsp:useBean id="beanId" ... /> ...
<jsp:getProperty name="beanId"
property="someProperty" .../>
<jsp:setProperty> sets the value of property in bean object.
<jsp:include> Includes another resources in current page at
runtime
<jsp:plugin> Embeds other components such as applets
<jsp:forward> Forwards request to another jsp
<jsp:param> sets the parameter value. It is used in
forward and include mostly.
E. Swathi, CSE, CBIT
<jsp:include> Example
includeAction.jsp
<html>
<head> //welcome.jsp
<html>
<title>include Action test</title>
<body>
<body> <h2>
<h1>include action example</h1> <% out.print("welcome to CBIT");
<h2>include directive</h2> %>
<%@ include file="welcome.jsp"%> </h2>
</body>
<h2> using include action</h2>
</html>
<jsp:include page="welcome.jsp"/>
</body>
</html>
Go to foloder includeAction
welcome.jsp
<html>
<body>
<%
String name= request.getParameter(“uname”);
out.println(“welcome to”+name);
%>
</body>
</html>
response implicit object
• instance of javax.servlet.http.HttpServletResponse object.
• Server creates an object to represent the response to the
client.
Firstpage.html
<html>
<body>
<form action=“welcome.jsp”>
<input type=“text” name=“uname”>
<input type= “submit” value=“go”></br>
</form>
</body> welcome.jsp
</html> <html>
<body>
<%
response.sendRedirect(http://www.google.com);
%>
</body>
</html>
out implicit object
➢ instance of a javax.servlet.jsp.JspWriter object.
example
<html>
<body>
<% out.print(“ today is”+java.util.Calander.getInstance().getTime());
%>
session
➢ used to track client across multiple client requests.
➢ instance of javax.servlet.http.HttpSession
• Created when jsp page is initialized and removed when jsp page
is terminated(jspDestroy()) method.
• With this object you can count the number of times a site is
visited.
<%=application.getInitParameter("User") %>
<%@ page import="java.io.*,java.util.*" %>
application example
<html>
<head>
<title>Applcation object in JSP</title>
</head>
<body>
<%
Integer hitsCount = (Integer)application.getAttribute("hitCounter");
if( hitsCount ==null || hitsCount == 0 ){
/* First visit */
out.println("Welcome to my website!");
hitsCount = 1;
}else{
/* return visit */
out.println("Welcome back to my website!");
hitsCount += 1;
}
application.setAttribute("hitCounter", hitsCount);
%>
<center>
<p>Total number of visits: <%= hitsCount%></p>
</center></body></html>
config
• get the JSP init params configured in deployment descriptor(web.xml).
• Instance of javax. servlet.ServletConfig
exception
Index.html
process.jsp
<form action="process.jsp">
<%@ page errorPage="error.jsp" %>
No1:<input type="text" name="n1"
<%
/><br/><br/>
No2:<input type="text" name="n2"
String num1=request.getParameter("n1");
/><br/><br/>
String
<input type="submit" value="divide"/>
num2=request.getParameter("n2");
</form>
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
Error.jsp
out.print("division of numbers is: "+c);
<%@ page isErrorPage="true" %>
%>
<h3>Sorry an exception occured!</h3>
erroPage- used to define error page
Exception is: <%= exception %> if error occurred in the current page
it will be redirected to the error page.
Go to Exception folder isErrorPage- declares the current page is
the error.
Page object
➢ JSP page implicit object is instance of java.lang.Object class
public static int EVAL_PAGE it evaluates the JSP page content after the custom
tag.
public static int SKIP_BODY it skips the body content of the tag.
public static int SKIP_PAGE it skips the JSP page content after the custom tag.
return SKIP_BODY;
}
<tag>
<name>today</name>
<tag-class>PrintDate</tag-class>
</tag>
</taglib>
<jsp-config>
<taglib>
<taglib-uri>mytags</taglib-uri>
<taglib-location>/WEB-INF/mytags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
index.jsp
<%@ taglib uri="mytags" prefix=“date" %>
Today is: <date:today></date:today>
//mytags.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>m</short-name>
<uri>mytags</uri>
<description>A simple tab library for the examples</description>
<tag>
<name>cube</name>
<tag-class>cCubeNumber</tag-class>
<attribute>
<name>number</name>
<required>true</required>
</attribute>
</tag>
</taglib> E. Swathi, CSE, CBIT
// CubeNumber.java
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
return SKIP_BODY;
}
} E. Swathi, CSE, CBIT
Model 1 and Model 2 (MVC) Architecture
• Before developing the web applications, we need to have idea about design
models. There are two types of programming models (design models)
• Model 1 Architecture
• Model 2 (MVC) Architecture
• Model 1 Architecture
• Servlet and JSP are the main technologies to develop the web applications.
• Servlet was considered superior to CGI. Servlet technology doesn't create
process, rather it creates thread to handle request. The advantage of
creating thread over process is that it doesn't allocate separate memory area.
Thus many subsequent requests can be easily handled by servlet.
• Problem in Servlet technology Servlet needs to recompile if any
designing code is modified. It doesn't provide separation of concern.
Presentation and Business logic are mixed up.
• These will be simple java objects , constructed by request handlers and set
as attributes in the request to make them accessible to view jsp pages using
<jsp:useBean> tag.
• Page beans won’t do markup generation or execute any logic. They will be
treated as read only objects by JSP views and will provide the model data
the views require.
• https://way2java.com/java-general/introduction-to-2-tier-and-
3-tier-architecture/
• https://way2java.com/
• https://www.studytonight.com/servlet/servlet-
api.phphttps://www.javatpoint.com/custom-tags