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

Java Server Pages

Java Server Pages (JSP) is a technology for creating dynamic web content by embedding Java code within HTML using specific tags. It allows for easier maintenance and faster development compared to Servlets, as changes do not require recompilation. The JSP life cycle includes translation to servlet code, compilation, initialization, execution, and destruction, with various JSP elements such as scriptlets, declarations, expressions, comments, and directives for different functionalities.

Uploaded by

ashokgouriprusty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java Server Pages

Java Server Pages (JSP) is a technology for creating dynamic web content by embedding Java code within HTML using specific tags. It allows for easier maintenance and faster development compared to Servlets, as changes do not require recompilation. The JSP life cycle includes translation to servlet code, compilation, initialization, execution, and destruction, with various JSP elements such as scriptlets, declarations, expressions, comments, and directives for different functionalities.

Uploaded by

ashokgouriprusty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Server Pages (JSP)

 Java Server Pages (JSP) is a technology for developing web pages that supports dynamic content.
 Developers can insert java code in HTML pages by making use of special JSP tags.
 JSP is used to create web application just like Servlet technology (extension to Servlets).
 JSP is easy to maintain because business logic can be separated from presentation logic.
 If JSP code is modified, there’s no need to recompile and redeploy. (Fast Development)
 It takes less code than Servlet

A Simple Example:

<html>
<head>
</head>
<body>
<% out.print(“Hello Silicon!”); %>
</body>
</html>

The file needs to be saved with a .jsp extension (e.g. hello.jsp). Put the file in a folder (e.g. mywebsite)
inside the web-apps folder in Apache Tomcat. Then you can browse to:

http://localhost:8080/mywebsite/hello.jsp.

JSP Processing

The following steps explain how the web server creates the Webpage using JSP −

 Browser sends an HTTP request for a .jsp page to the web server.
 The web server recognizes that the request is for a JSP page and forwards it to the JSP engine.
 The JSP engine locates the jsp file from the disk and loads it into memory, and converts it into
a servlet. All template text is converted to println() statements and all JSP elements are
converted to Java code.
 Then JSP engine compiles the servlet into an executable class and forwards the original request
to a servlet engine, which loads the servlet class and executes it.
 During execution, the servlet produces output in HTML format. The output is passed on to the
web server as the HTTP response.
 The web server then forwards the HTTP response to the browser as HTML content.
 Finally, the web browser displays the HTML page as usual.

Life-Cycle of a JSP Page

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.
• Translation of JSP to Servlet code: The JSP Engine reads the .jsp file and generates its servlet
code automatically.

• Compilation of Servlet code: The servlet code generated is then compiled into bytecode
generating the java .class file.

• Loading Servlet class: The compiled servlet class is loaded by the servlet engine and an
instance of the servlet is created.

• Initialization: The servlet is initialized by calling jspInit() method. If you want to write any
specific initialization code, then you can override this method.

• Execution & Request Processing: The request is processed by calling _jspService() method.
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. This method cannot be overridden.

• Destruction & Cleanup: Finally, the servlet is destroyed by calling jspDestroy() method. If
you want any specific cleanup code (such as releasing database connections), then you can
override this method.
JSP Elements
Elements of JSP code are embedded within HTML code using different types of tags. There are five
types of elements which use their own tag syntax.

1. JSP Scriptlet: The scriptlet element uses <%... %> tag syntax. A scriptlet can contain any number of
JAVA language statements, variable or method declarations, or expressions that are valid in the page
scripting language. Any text, HTML tags, or other JSP elements must be outside the scriptlet.

Example:

<html>
<head><title>Hello World</title></head>
<body>
<h1>Hello World!</h1>
<%
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. Variables or methods must be declared before they are used in the JSP file.
The declaration element uses the <%! …%> tag syntax.

Example:

<%! int i = 0; %>

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. The
expression element can contain any expression that is valid in the Java language but you cannot use a
semicolon to end an expression. It uses the <%= … %> tag syntax.

Example:

<html>
<head><title>Date Example</title>
</head>
<body>
<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
</body>
</html>

4. JSP Comments: JSP comment marks a portion of code or statements that the JSP engine should
ignore. It uses the <%-- … --%> tag syntax. Anything within comments is not translated.
5. JSP Directives: A JSP Directive is an instruction to the JSP engine how to translate a JSP page into
the corresponding servlet. It affects the overall structure of the servlet class. A JSP directive uses the
<%@ … %> tag syntax. There are three types of JSP directives:

a. page Directive: It defines page dependent attributes which apply to the entire JSP page. It uses the
<%@ page attribute="value" %> syntax. Example: <%@page author=”Suresh Kumar” %>

b. include Directive: The include directive is used to include the contents of any other resource (text,
html, or even JSP) inside the current JSP file during the translation phase. Example:
<%@include file=”header.html” %>

c. taglib Directive: The taglib directive is used to define a custom tag library. Example:

<html>
<body>
<%@ taglib uri="http://www.silicon.ac.in/tags" prefix="mytag" %>
<mytag:currentDate />
</body>
</html>

You might also like