0% found this document useful (0 votes)
210 views

Advance Java Unit 4 JSP Sem6 Bca

The document discusses JSP (JavaServer Pages) technology. It begins by introducing JSP and how it is an extension of Servlet technology that provides additional functionality. It then discusses some key advantages of JSP over Servlets, including being easier to maintain by separating design from development, not requiring recompilation for minor changes, and using tags to reduce code. Finally, it outlines the lifecycle of a JSP page, including translation to a servlet, compilation, class loading, initialization, request processing, and destruction.

Uploaded by

aishwarya saji
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views

Advance Java Unit 4 JSP Sem6 Bca

The document discusses JSP (JavaServer Pages) technology. It begins by introducing JSP and how it is an extension of Servlet technology that provides additional functionality. It then discusses some key advantages of JSP over Servlets, including being easier to maintain by separating design from development, not requiring recompilation for minor changes, and using tags to reduce code. Finally, it outlines the lifecycle of a JSP page, including translation to a servlet, compilation, class loading, initialization, request processing, and destruction.

Uploaded by

aishwarya saji
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

JSP

CHAPTER 4
INTRODUCTION
• 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 Tags, etc.
• Jakarta Server Pages (JSP; formerly JavaServer Pages) is a collection of technologies that helps software developers create
dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun
Microsystems, JSP is similar to PHP and ASP, but uses the Java programming language.
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.
Lifecycle of a JSP Page
• JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP Page needs to be converted into servlet first in
order to process the service requests. The Life Cycle starts with the creation of JSP and ends with the disintegration of that.

• Following steps explain the JSP life cycle:


• Translation of JSP page
• Compilation of JSP page(Compilation of JSP page into _jsp.java)
• Classloading (_jsp.java is converted to class file _jsp.class)
• Instantiation(Object of generated servlet is created)
• Initialisation(_jspinit() method is invoked by container)
• Request Processing(_jspservice() method is invoked by the container)
• Destroy (_jspDestroy() method invoked by the container)
Lifecycle of a JSP Page
Lifecycle of a JSP Page
1. Translation of the JSP Page:

• A Java servlet file is generated from a JSP source file. This is the first step of JSP life cycle. In translation phase,
container validates the syntactic correctness of JSP page and tag files.
• The JSP container interprets the standard directives and actions, and the custom actions referencing tag libraries (they
are all part of JSP page and will be discussed in the later section) used in this JSP page.
• In the above pictorial description, demo.jsp is translated to demo_jsp.java in the first step

• Let’s take an example of “demo.jsp” as shown below:


Demo.jsp
• <html>
• <head>
• <title>Demo JSP</title>
• </head>
• <%
• int demvar=0;%>
• <body>
• Count is:
• <% Out.println(demovar++); %>
• <body>
• </html>
Code Explanation for Demo.jsp
• Code Line 1: html start tag
• Code Line 2: Head tag
• Code Line 3 – 4: Title Tag i.e. Demo JSP and closing head tag
• Code Line 5,6: Scriptlet tag wherein initializing the variable demo
• Code Line 7 – 8: In body tag, a text to be printed in the output (Count is: )
• Code Line 9: Scriplet tag where trying to print the variable demovar with incremented value
• Code Line 10-11: Body and HTML tags closed

• Demo JSP Page is converted into demo_jsp servlet in the below code.
Demo JSP Page is converted into demo_jsp servlet in the below code
Code explanation for Demo_jsp.java
• Code Line 1: Servlet class demo_jsp is extending parent class HttpServlet
• Code Line 2,3: Overriding the service method of jsp i.e. _jspservice which has HttpServletRequest and
HttpServletResponse objects as its parameters
• Code Line 4: Opening method
• Code Line 5: Calling the method getWriter() of response object to get PrintWriterobject (prints formatted
representation of objects to text output stream)
• Code Line 6: Calling setContentType method of response object to set the content type
• Code Line 7: Using write () method of PrintWriter object trying to parse html
• Code Line 8: Initializing demovar variable to 0
• Code Line 9: Calling write() method of PrintWriter object to parse the text
• Code Line 10: Calling print() method of PrintWriter object to increment the variable demovar from 0+1=1.Hence, the
output will be 1
• Code Line 11: Using write() method of PrintWriter object trying to parse html
output

Here you can see that in the screenshot theOutput is 1 because demvar is initialized to 0 and then incremented to
0+1=1
In the above example,

demo.jsp, is a JSP where one variable is initialized and incremented. This JSP is converted to the servlet
(demo_jsp.class ) wherein the JSP engine loads the JSP Page and converts to servlet content.
When the conversion happens all template text is converted to println() statements and all JSP elements are
converted to Java code.
This is how a simple JSP page is translated into a servlet class.
Lifecycle of a JSP Page
2. Compilation of the JSP Page
• The generated java servlet file is compiled into java servlet class
• The translation of java source page to its implementation class can happen at any time between the deployment of JSP
page into the container and processing of the JSP page.
• In the above pictorial description demo_jsp.java is compiled to a class file demo_jsp.class
3. Classloading
• Servlet class that has been loaded from JSP source is now loaded into the container
4. Instantiation
• In this step the object i.e. the instance of the class is generated.
• The container manages one or more instances of this class in the response to requests and other events. Typically, a
JSP container is built using a servlet container. A JSP container is an extension of servlet container as both the
container support JSP and servlet.
• A JSPPage interface which is provided by container provides init() and destroy () methods.
• There is an interface HttpJSPPage which serves HTTP requests, and it also contains the service method.
Initialization
• public void jspInit()
• {
• //initializing the code
• }
• _jspinit() method will initiate the servlet instance which was generated from JSP and will be invoked by the container in
this phase.
• Once the instance gets created, init method will be invoked immediately after that
• It is only called once during a JSP life cycle, the method for initialization is declared as shown above
Request processing
• void _jspservice(HttpServletRequest request HttpServletResponse response)
• {
• //handling all request and responses
• }
• jspservice() method is invoked by the container for all the requests raised by the JSP page during its life cycle
• For this phase, it has to go through all the above phases and then only service method can be invoked.
• It passes request and response objects
• This method cannot be overridden
• The method is shown above: It is responsible for generating of all HTTP methods i.eGET, POST, etc.
Destroy
• public void _jspdestroy()
• {
• //all clean up code
• }
• jspdestroy() method is also invoked by the container
• This method is called when container decides it no longer needs the servlet instance to service requests.
• When the call to destroy method is made then, the servlet is ready for a garbage collection
• This is the end of the life cycle.
• We can override jspdestroy() method when we perform any cleanup such as releasing database connections or closing
open files.
JSP Scripting Element

• 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.
• Example-
• <html>
• <head>
• <title>My First JSP Page</title>
• </head>
• <%
• int count = 0;
• %>
• <body>
• Page Count is <% out.println(++count); %>
• </body>
• </html>
There are five different types of scripting elements

Scripting Element Example

Comment <%-- comment --%>

Directive <%@ directive %>

Declaration <%! declarations %>

Scriptlet <% scriplets %>

Expression <%= expression %>


JSP Comment
• JSP Comment is used when you are creating a JSP page and want to put in comments about what you are doing. JSP
comments are only seen in the JSP page. These comments are not included in servlet source code during translation phase,
nor they appear in the HTTP response. Syntax of JSP comment is as follows :
• <%-- JSP comment --%>
• Simple Example of JSP Comment
• <html>
• <head>
• <title>My First JSP Page</title>
• </head>
• <%
• int count = 0;
• %>
• <body>
• <%-- Code to show page count --%>
• Page Count is <% out.println(++count); %>
• </body>
• </html>
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 %>

• JSP: Example of Scriptlet


• In this example, we will show number of page visit.
• <html>
• <head>
• <title>My First JSP Page</title>
• </head>
• <%
• int count = 0;
• %>
• <body>
• Page Count is <% out.println(++count); %>
• </body>
• </html>
• We have been using the above example since last few lessons and in this scriptlet tags are used. Everything written inside
the scriptlet tag is compiled as java code. Like in the above example, we initialize count variable of type int with a value of
0. And then we print it while using ++ operator to perform addition on it.

• JSP makes it so easy to perform calculations, database interactions etc directly from inside the HTML code. Just write your
java code inside the scriptlet tags.

• 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>
EXAMPLE
• 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.
• 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>
EXAMPLE
• 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.
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>
• 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>");

• out.write("Page count is:");
• out.print(++count);
• out.write("</body></html>");
• }
• }
JSP Directive Tag
• Directive Tag gives special instruction to Web Container at the time of page translation. Directive tags are of three types:
page, include and taglib.

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
JSP Directive Tag
• The Page directive defines a number of page dependent properties which communicates with the Web Container at the
time of translation.
• Basic syntax of using the page directive is <%@ page attribute="value" %> where attributes can be one of the following :

• import attribute
• language attribute
• extends attribute
• session attribute
• isThreadSafe attribute
• isErrorPage attribute
• errorPage attribute
• contentType attribute
• autoFlush attribute
• buffer attribute
 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.

• Syntax of JSP expression tag


• <%= statement %>
• Example of JSP expression tag
• In this example of jsp expression tag, we are simply displaying a welcome message.

• <html>
• <body>
• <%= "welcome to jsp" %>
• </body>
• </html>

You might also like