0% found this document useful (0 votes)
8 views25 pages

Java Server Pages: Problems With Servlets

The document discusses the limitations of Servlets, such as the need for recompilation when changing presentation logic and the complexity of integrating HTML with Java code. It introduces Java Server Pages (JSP) as a solution that separates business logic from presentation logic, allowing for easier development and maintenance. Additionally, it outlines the anatomy of a JSP file, its processing phases, and the MVC design pattern used in JSP applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Java Server Pages: Problems With Servlets

The document discusses the limitations of Servlets, such as the need for recompilation when changing presentation logic and the complexity of integrating HTML with Java code. It introduces Java Server Pages (JSP) as a solution that separates business logic from presentation logic, allowing for easier development and maintenance. Additionally, it outlines the anatomy of a JSP file, its processing phases, and the MVC design pattern used in JSP applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Java Server Pages

Problems with Servlets

The Servlet itself contains the code for all aspects of the
application like
Request processing,
Business logic &
Presentation logic.

html code(presentation logic) is embedded inside


java code(business logic).
Problems with Servlets (contd..)

out.println("<html>");
out.println("<body>");
out.println("<b>servlet can use htmltags</b><br>");
out.println("<font color=red>");
out.println("hello shivaram");
out.println("</font></body>");
out.println("</html>");
1. Changes in the look and feel (presentation) of the
application requires the Servlet to be updated and
recompiled.

2. Developer needs proficiency in both web page


designing and java.

3. Difficult to use development tools.


for example, if we use a tool to develop a user
interface design, the generated html code has to be
manually inserted with in the java code which is time
consuming and error prone.
Java Server Pages - Advantages
1. JSP Separates business logic and presentation logic.
i) Presentation logic(html code) can be written in a
jsp file directly(as if you are writing in a HTML file).
ii) Business logic (java code) should be written
inside JSP elements.

2. JSP has built objects, which helps in reducing


development effort.
Anatomy of a JSP file
JSP File consists of
1. Template text
it can be plane text, html code, XML code or any
other markup language code.
2. JSP Elements
i) Scripting elements
( declarations, expressions, scriptlets)
ii) Directives
(page, include & taglib)
iii) Actions
iv) EL (Expression Language) expressions.
v) Comments.
Anatomy of a JSP file (contd..)
When a JSP file is requested by the client,
the template text is merged with
dynamic content generated by JSP elements
and sent as a response back to the client
JSP Processing
Translation phase:
When the first client sends a request for a JSP
file, JSP file will be converted into a Servlet.

.
The “.jsp ” converted to “ java”

i) The html code present in the JSP file will


be converted to the corresponding println()
statements.

ii) The JSP elements will be converted to the


java code that implements the
corresponding dynamic behaviour.
.
The converted “ java” file is compiled and generates
“.class” file.

This “.class” file is called as jsp page implementation class.

Then an instance for the jsp page implementation class is


created.

Request processing phase:


The container invokes service() to process client
request.
Because of the translation phase the first client will face
some delay in getting the response.

To avoid this delay some JSP containers will use the


mechanism of pre compilation ie executing the
translation phase even before the first client send a
request for a jsp file.

From the second client request onwards the container will


only execute request processing phase.
MVC Design Pattern

1. Model (Business Logic)


2. View (Presentation Logic)
3. Controller (Request Processing)
1. Model
Represents Business logic.

Responsible for generating the content(data) what the


client has requested for.

Generally the java code that access the data from


Database(if required) , process the data and generates the
response requested by the client.

Technologies used are


i) Java Beans
ii) Enterprise JavaBeans
iii) POJO (Plane Old Java Objects)
2. View
Represents Presentation logic.

Responsible for presenting the content(data) generated


by the Model components.

We can have more than on View component for one


Model component.

Technologies used are


i) HTML
ii) JSP
3. Controller
Represents Request Processing.

The Controller is responsible for controlling the


application logic and acts as the coordinator between
the View and the Model.

The Controller receives an input from the users via the


View, then processes the user's data with the help of
Model and passes the results back to the View for
displaying the results to client browser.
JSP Elements
1.Directives
Directives are translation time instruction to the JSP
container.
page directive
<%@ page language="java"
contentType="text/html”
extends="demotest.DemoClass“
import="java.util.Date“
session=“true/false“
isThreadSafe="true/false“
isErrorPage=“true/false“
errorPage="errorHandler.jsp“%>
JSP Elements
1.Directives (contd..)
include directive
1. Used to include one file in another file.
2. It includes file during translation phase.
Example:
<%@ include file=“directive_header_jsp3.jsp”%>
JSP Elements
1.Directives (contd..)
taglib directive
The JSP API allow you to define custom JSP tags that look
like HTML or XML tags and a tag library is a set of
user-defined tags that implement custom behavior.
The taglib directive declares that your JSP page uses a set
of custom tags, identifies the location of the library, and
provides means for identifying the custom tags in your
JSP page.
Syntax:
<%@ taglib uri = "uri" prefix = "prefixOfTag" >
JSP Elements
2. JSP Scripting Elements

1. Declarations
2. Scriptlets
3. Expressions
JSP Elements
2. JSP Scripting Elements
Declarations:
A declaration tag is a piece of Java code for declaring
variables and methods.
If we declare a variable or method inside declaration tag it
means that the declaration is made inside the Servlet
class but outside the service() method.
Example
<%! int count =10; %>
JSP Elements
2. JSP Scripting Elements
Scriptlets:
1. Allows you to write Java code in JSP file.
2. JSP container moves statements in _jspservice()
method while generating Servlet from JSP.
Example:
<%
int num1=10;
int num2=40;
int num3 = num1+num2;
out.println("Scriplet Number is " +num3);
%>
JSP Elements
2. JSP Scripting Elements
Expressions
1. Evaluates the expression and send it as parameter to
out.println().
2. It allows to create expressions like arithmetic and
logical.
3. It produces scriptless JSP page.
Example:
<%= num1*num2+num3 %>
Equivalent to
out.println(num1*num2+num3);
JSP Elements
3.Actions
We can dynamically
insert a file,
reuse the bean components,
forward user to another page, etc. through JSP Actions
Unlike directives, actions are re-evaluated each time the page
is accessed.

Example
<jsp:useBean id="name“ class="demotest.DemoClass">
<jsp:include page="date.jsp“ />
<jsp:forward page="jsp_action_42.jsp" />
JSP Elements
4.Comments

Add JSP comment as


<%-- Write your comment here --%>

Add HTML comment as


<!-- Write your comment here -->
JSP Built in Objects
request
response
out
config
exception
session
application
page
pagecontext
JSP Built in Objects – Servlet Classes

request - HttpServletRequest
response - HttpServletResponse
out - JSPWriter
config - ServletConfig
exception - Throwable
session - HttpSession
application - ServletContext

You might also like