Module_5_Introduction to JSP_ JSTL and EL
Module_5_Introduction to JSP_ JSTL and EL
2
Advantages of JSP over Servlet:
• There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet:
JSP technology is the extension to Servlet technology. We can use all the features of the 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.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code needs to
be updated and recompiled if we have to change the look and feel of the application.
4) Less code than Servlet:
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code.
Moreover, we can use EL, implicit objects, etc.
3
Features of JSP
•Coding in JSP is Easy: As it involves adding Java code to HTML/XML.
•Easy to Use and Learn: It is straightforward and accessible for both Java and non-Java programmers.
•It Does Not Require Advanced Knowledge of Java: Suitable for users with basic Java skills.
•Reduction in the Length of Code: JSP uses action tags, custom tags, etc., to minimize code length.
•Java Scriplets: Allows embedding Java code, variables, and expressions within JSP pages.
•JSP Expression: Evaluates expressions and converts them to strings.
•Declaration Tag: Used to declare variables and methods within JSP pages.
•Implicit Objects: Provides built-in objects that reduce the length of code.
•Make Interactive Websites: Facilitates the creation of dynamic web pages that interact with users in
real-time.
•Connection to Database is Easier: Simplifies connecting to databases and allows for easy data reading
and writing.
•Extension to Servlet: Inherits all features of servlets and includes implicit objects and custom tags.
•Portable, Powerful, Flexible, and Easy to Maintain: Browser and server independent, making it
versatile and easy to manage.
•No Redeployment and No Re-Compilation: JSP is dynamic, secure, and platform-independent, so4 no
JSP Architecture
• Following diagram shows the position of JSP container and JSP files in a Web application.
5
JSP Processing
The following steps explain how the web server creates the Webpage using JSP −
• Initially, your browser sends an HTTP request to the web server.
• The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine.
This is done by using the URL or JSP page which ends with .jsp instead of .html.
• The JSP engine loads the JSP page from disk and converts it into a servlet content. In this conversion,
all template text are converted into println( ) statements and all JSP elements are converted to Java
code. This code implements the corresponding dynamic behavior of the page.
• The JSP engine compiles the servlet into an executable class and forwards the original request to a
servlet engine.
• A part of the web server called the servlet engine loads the Servlet class and executes it. During
execution, the servlet produces an output in HTML format. The output is further passed on to the web
server by the servlet engine inside an HTTP response.
• The web server forwards the HTTP response to your browser in terms of static HTML content.
• Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response
exactly as if it were a static page.
6
• All the above mentioned steps can be seen in the following diagram −
7
Lifecycle of a JSP Page
The JSP pages follow these phases:
• Translation of JSP Page
• Compilation of JSP Page
• Classloading (the classloader loads class file)
• Instantiation (Object of the Generated Servlet is created).
• Initialization ( the container invokes jspInit() method).
• Request processing ( the container invokes _jspService() method).
• Destroy ( the container invokes jspDestroy() method).
8
9
Scripting Elements of JSP
• 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.
10
JSP Scriptlet Tag
• Scriptlet Tag allows you to write java code inside JSP page.
• Scriptlet tag implements the _jspService method functionality by writing script/java code. Syntax of
Scriptlet Tag is as follows :
<% JAVA CODE %>
• Example of Scriptlet:
<html>
<head>
<title>My First JSP Page</title>
</head>
<%
int count = 0;
%>
<body>
Page Count is <% out.println(++count); %>
</body>
</html>
11
• Example of JSP Scriptlet Tag:
• in this example, we will create a simple JSP page which retrieves the name of the user from the request
parameter. The index.html page will get the username from the user.
index.html
<form method="POST" action="welcome.jsp">
Name <input type="text" name="user" >
<input type="submit" value="Submit">
</form>
• In the above HTML file, we have created a form, with an input text field for user to enter his/her name,
and a Submit button to submit the form. On submission an HTTP Post request ( method="POST" ) is
made to the welcome.jsp file ( action="welcome.jsp" ), with the form values.
12
welcome.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<%
String user = request.getParameter("user");
%>
<body>
Hello, <% out.println(user); %>
</body>
</html>
• As we know that a JSP code is translated to Servlet code, in which _jspService method is executed
which has HttpServletRequest and HttpServletResponse as argument. So in the welcome.jsp file,
request is the HTTP Request and it has all the parameters sent from the form in index.html page, which
we can be easily get using getParameter() with name of parameter as argument, to get its value.
13
• JSP Comment
• JSP Comment is used when you are creating a JSP page and want to put in comments about what you
are doing.
• Syntax of JSP comment is as follows :
<%-- JSP comment --%>
• Simple Example of JSP Comment:
<html>
<head>
<title>My First JSP Page</title>
</head>
<body>
<%-- Code to show page count --%>
Page Count is <% out.println(++count); %>
</body>
</html>
14
JSP Declaration Tag
• We know that at the end a JSP page is translated into Servlet class. So when we declare a variable or
method in JSP inside Declaration Tag, it means the declaration is made inside the Servlet class but
outside the service(or any other) method.
• You can declare static member, instance variable and methods inside Declaration Tag. Syntax of
Declaration Tag :
<%! declaration %>
• Example of Declaration Tag
<html>
<head>
<title>My First JSP Page</title>
</head>
<%!
int count = 0;
%>
<body>
Page Count is:
<% out.println(++count); %>
</body>
</html>
15
I
• In the above code, we have used the declaration tag to declare variable count. The above JSP page
becomes this Servlet :
public class hello_jsp extends HttpServlet
{
int count=0;
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out = response.getWriter();
response.setContenType("text/html");
out.write("<html><body>");
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
17
I
• In
18
I
• In
19
I
• In
20
I
• In
21