Professional Documents
Culture Documents
JSP
JSP
CO 5 Building enterprise level applications and manipulate web databases using JDBC K3, K4
CO6 Design interactive web applications using Servlets and JSP K2, K3
Subject:Web
Subject: Web Technology
Technology
Syllabus
Unit-5
Servlets: Servlet Overview and Architecture, Interface Servlet and the
Servlet Life Cycle, Handling HTTP get Requests, Handling HTTP post
Requests, Redirecting Requests to Other Resources, Session Tracking,
Cookies, Session Tracking with Http Session
Java Server Pages (JSP): Introduction, Java Server Pages Overview, A First
Java Server Page Example, Implicit Objects, Scripting, Standard Actions,
Directives, Custom Tag Libraries.
Subject:Web
Subject: Web Technology
Technology
Subject: Web Technology
Subject: Web Technology
JSP
JSP was developed by Sun Microsystems and is an improved version of Java servlets.
JSP is translated into Java servlet before being run, and it processes only HTTP requests and
generates responses like any servlet.
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.
• Though the database can be accessed with JSP, it is not easy to access the database as most of the servlet
does not provide support.
• Being a servlet, if there’s an issue in the code, it becomes very hard to trace.
• It compilation time required is more than on a server.
Hello.jsp
HTTP request [1] Generate
Read
JSP page
helloServlet.java
HTML page
DB
Step 2. If the compiled version of JSP exists in the web server, it returns the file. Otherwise, the request is
forwarded to the JSP Engine. This is done by recognizing the URL ending with .jsp extension.
Step 3. The JSP Engine loads the JSP file and translates the JSP to Servlet(Java code). This is done by
converting all the template text into println() statements and JSP elements to Java code. This
process is called translation.
Step 4. The JSP engine compiles the Servlet to an executable .class file. It is forwarded to the Servlet
engine. This process is called compilation or request processing phase.
Step 5. The .class file is executed by the Servlet engine which is a part of the Web Server. The output is an
HTML file. The Servlet engine passes the output as an HTTP response to the webserver.
Step 6. The web server forwards the HTML file to the client’s browser.
Subject: Web Technology
Architecture of JSP
JSP architecture is a three-tier architecture consisting of a web server, client, and database.
Web Server: It uses a JSP engine like a container that processes JSP. E.g., Apache Tomcat. This engine intercepts
the request for JSP and provides a runtime environment for the understanding and processing of JSP files. It
reads, parses, builds Java Servlet, Compiles, Executes Java code, and returns the HTML page to the client.
Database: It is used to store the user’s data. The webserver has access to the database.
1 2
Java Bean
JSP 3
4
Database
Controller
View Model
View
View
View
Response
Step
Step
Step
Step 35
42 1
Step
Response
View
Model isprocesses
is passed
Incoming
Controller rendered
transforms Model
to View
request into appropriate
directed
request to forms aoutput
andController. format
data Model
Model-View-Controller (MVC)
CONTROLLER
Request
Browser
Servlet
MODEL
DB
Java Bean
Enterprise Server/
VIEW
Response Data Sources
Jsp
Application Server
• Separating Model from View (that is, separating data representation from
presentation)
• easy to add multiple data presentations for the same data, facilitates adding new
types of data
• Extensibility
• Testability.
index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page.
We will learn scriptlet tag later.
1. <html>
2. <body>
3. <% out.print(2*5); %>
4. </body>
5. </html>
The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-INF
folder or in any directory.
Web container: A web container is J2EE compliant implementation. Its main job is to run the runtime
environment to JSP and servlets. Request receiver at web browser is forwarded here, and the result
generated is sent back to the web server. E.g., Tomcat.
2) JSP Compilation
Prior to compilation, JSP engine probes if the page has ever been compiled.
If the page has some modifications or if it has never been compiled, then JSP engine compiles the
page.
3) Class Loading
At this stage, the .class file gets loaded by the loader i.e., corresponding servlet class file is loaded.
5) Initialization
After instantiation, container invokes jspInit() method better known as initialization of JSP.
It basically initializes the instance we created. It is done to initialize resources needed to process
the request i.e. resources like databases, allow JSP pages to read data, create lookup tables, and
manage network connections.
Though it is invoked by the container only once, it can be overridden by the author.
This service method is responsible for generating a response to the request. Not only this, but it also
generates response to all seven HTTP methods, namely:
a) GET
GET is a read only request. It tends to get information from the server. GET request posses 3
standard triggers:
• Typing in the address line of the browser and clicking enter or go.
• Pressing submit button in HTML form.
• Clicking on the link of a web page in order to access it.
Subject: Web Technology
b) POST
This method gets triggered when the form settings is made as a post.
c) HEAD
This method is almost similar to the GET method. Though this method doesn’t return a message body, it
can check if a resource hasn’t been recently updated, valid, and accessible.
d) PUT
Using the request payload, PUT method replaces current representations of the target resource.
e) DELETE
This method deletes the specified resource.
f) CONNECT
This method establishes a path identified by the target resource to reach the server.
g) OPTIONS
This method describes the communication options that we need to communicate with the target resource.
Conclusion
Finally we have seen JSP Life cycle, various phases of JSP Lifecycle and methods invoked in it:
• Web container translates JSP code into servlet class source (.java)
• It is compiled into java servlet class.
• Class Loader loads Servlet class bytecode.
• Container creates instance of that servlet class
• Initialize servlet can now service requests.
• _jspService() is invoked by web container for each request.
• To remove the servlet instance jspDestroy() is called. It performs cleanup.
jsp:param sets the parameter value. It is used in forward and include mostly.
jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.
Actions
Predefined tasks that are processed by the JSP container at request time.
<jsp:include> Action
Includes a file at the time the page is requested.
<jsp: include page="banner.html" flush = "true" />
<jsp:useBean> Action
Declares a Java Bean instance for use in the JSP page.
<jsp:useBean id="courseBean"
class="coursepack.CourseListBean" />
Equivalent to expression:
<%= courseBean.getCourseColor(courseNumber) %>
Equivalent to expression:
<%= courseBean.setCourseColor("red") %>