WT - Unit - 5
WT - Unit - 5
A JSP container works with the Web server to provide the runtime environment and other services a JSP
needs. It knows how to understand the special elements that are part of JSPs.
Following diagram shows the position of JSP container and JSP files in a Web application.
JSP Processing
The following steps explain how the web server creates the Webpage using JSP −
As with a normal page, 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. This
conversion is very simple in which all template text is converted to 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 furthur 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.
All the above mentioned steps can be seen in the following diagram –
Typically, the JSP engine checks to see whether a servlet for a JSP file already exists and whether the
modification date on the JSP is older than the servlet. If the JSP is older than its generated servlet, the
JSP container assumes that the JSP hasn't changed and that the generated servlet still matches the JSP's
contents. This makes the process more efficient than with the other scripting languages (such as PHP)
and therefore faster.
So in a way, a JSP page is really just another way to write a servlet without having to be a Java
programming wiz. Except for the translation phase, a JSP page is handled exactly like a regular servlet.
JSP - Lifecycle
A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet
life cycle with an additional step which is required to compile a JSP into servlet.
Paths Followed By JSP
The following are the paths followed by a JSP −
Compilation
Initialization
Execution
Cleanup
The four major phases of a JSP life cycle are very similar to the Servlet Life Cycle. The four phases have
been described below −
JSP Compilation
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If
the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP
engine compiles the page.
The compilation process involves three steps −
Parsing the JSP.
Turning the JSP into a servlet.
Compiling the servlet.
JSP Initialization
When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to
perform JSP-specific initialization, override the jspInit() method −
public void jspInit()
{
// Initialization code...
}
JSP Execution
This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes
the _jspService() method in the JSP.
Elements of JSP
The elements of JSP have been described below −
1. The Scriptlet
A scriptlet can contain any number of JAVA language statements, variable or method declarations, or
expressions that are valid in the page scripting language.
Syntax of Scriptlet −
<% code fragment %>
Note: Any text, HTML tags, or JSP elements you write must be outside the scriptlet.
Example
<html>
<head><title>Hello World</title></head>
<body>
Hello World! <br>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
2. JSP Declarations
A declaration declares one or more variables or methods that you can use in Java code later in the JSP
file. You must declare the variable or method before you use it in the JSP file.
syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
Example
3. JSP Expression
A JSP expression element contains a scripting language expression that is evaluated, converted to a
String, and inserted where the expression appears in the JSP file.
Because the value of an expression is converted to a String, you can use an expression within a line of
text, whether or not it is tagged with HTML, in a JSP file.
The expression element can contain any expression that is valid according to the Java Language
Specification but you cannot use a semicolon to end an expression.
Syntax of JSP Expression −
<%= expression %>
Example
<html>
<head><title>A Comment Test</title></head>
<body>
<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
</body>
</html>
4. JSP Comments
JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful
when you want to hide or "comment out", a part of your JSP page.
5. JSP Directives
A JSP directive affects the overall structure of the servlet class.
6. JSP Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can
dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate
HTML for the Java plugin.
Action elements are basically predefined functions. Following table lists out the available JSP Actions –
S.No. Syntax & Purpose
jsp:include
1
Includes a file at the time the page is requested.
jsp:useBean
2
Finds or instantiates a JavaBean.
jsp:setProperty
3
Sets the property of a JavaBean.
jsp:getProperty
4
Inserts the property of a JavaBean into the output.
jsp:forward
5
Forwards the requester to a new page.
jsp:plugin
6 Generates browser-specific code that makes an OBJECT or EMBED tag for the
Java plugin.
jsp:element
7
Defines XML elements dynamically.
jsp:attribute
8
Defines dynamically-defined XML element's attribute.
jsp:body
9
Defines dynamically-defined XML element's body.
jsp:text
10
Used to write template text in JSP pages and documents.
Anatomy of JSP
request
1
This is the HttpServletRequest object associated with the request.
response
2
This is the HttpServletResponse object associated with the response to the client.
out
3
This is the PrintWriter object used to send output to the client.
session
4
This is the HttpSession object associated with the request.
application
5
This is the ServletContext object associated with the application context.
config
6
This is the ServletConfig object associated with the page.
pageContext
7
This encapsulates use of server-specific features like higher performance JspWriters.
page
8 This is simply a synonym for this, and is used to call the methods defined by the
translated servlet class.
Exception
9
The Exception object allows the exception data to be accessed by designated JSP.
JavaBeans Properties
A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute
can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed
through two methods in the JavaBean's implementation class –
getPropertyName()
1 For example, if property name is firstName, your method name would
be getFirstName() to read that property. This method is called accessor.
setPropertyName()
2 For example, if property name is firstName, your method name would
be setFirstName() to write that property. This method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have
only a setPropertyName() method.
Example: (StudentsBean.java)
package com;
public class StudentsBean implements java.io.Serializable
{
private String firstName = null;
private String lastName = null;
public StudentsBean()
{
}
//Get Methods
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
//Set Methods
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
Accessing JavaBeans
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting
variable that can be accessed by both scripting elements and other custom tags used in the JSP. The full
syntax for the useBean tag is as follows –
<jsp:useBean id = "bean's name" />
The value of the id attribute may be any value as a long as it is a unique name among other useBean
declarations in the same JSP.
Example shows how to use the useBean action −
<html>
<head>
<title>useBean Example</title>
</head>
<body>
<jsp:useBean id = "date" class = "java.util.Date" />
<p>The date/time is <%= date %>
</body>
</html>
The name attribute references the id of a JavaBean previously introduced to the JSP by the useBean
action. The property attribute is the name of the getor the set methods that should be invoked.
Example (StudentsBean.jsp) shows how to access the data using the above syntax −
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<%@ page import="com.StudentsBean"%>
<html>
<head>
<title>get and set properties Example</title>
</head>
<body>
<jsp:useBean id = "students" class = "com.StudentsBean">
<jsp:setProperty name = "students" property = "firstName" value =
"Eeswar"/>
<jsp:setProperty name = "students" property = "lastName" value =
"Banala"/>
</jsp:useBean>
</body>
</html>
W11.html
<html>
<head> <title> SESSION LOGIN </title> </head>
<body>
<center>
<form action="w11a.jsp" method="get">
Enter Name: <input type="text" name="uname"> <br><br>
<input type="submit" value="LOGIN" name="register">
</form>
</center>
</body>
</html>