0% found this document useful (0 votes)
13 views20 pages

Why Use Java Serverpages?

java servelet notes

Uploaded by

john.dow.1024.05
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)
13 views20 pages

Why Use Java Serverpages?

java servelet notes

Uploaded by

john.dow.1024.05
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/ 20

Why use Java ServerPages?

JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway
Interface (CGI). But JSP offers several advantages in comparison with the CGI.
Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages
itself instead of having separate CGI files.

JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server
to load an interpreter and the target script each time the page is requested.

JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the
powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.

JSP pages can be used in combination with servlets that handle the business logic, the model supported by
Java servlet template engines.

Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This
means that JSP can play a part in the simplest applications to the most complex and demanding.
Difference between Servlet and JSP is as follows:

LifeCycle of a JSPPage
0

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.
The _jspService() method takes an HttpServletRequest and an
HttpServletResponse as its parameters as follows −
void _jspService(HttpServletRequest request, HttpServletResponse response) {
// Service handling code...
}

JSP Cleanup
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by
a container.
The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override
jspDestroy when you need to perform any cleanup, such as releasing database connections or
closing open files.

The jspDestroy() method has the following form −


public void jspDestroy() {
// Your cleanup code goes here.
}
How jsp works?
Step 1: The client navigates to a file ending with the .jsp extension

and the browser initiates an HTTP request to the webserver. For

example, the user enters the login details and submits the button. The

browser requests a status.jsp page from the webserver.

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.

Java ServerPages:Comments

Following example shows the JSP Comments −

<html>
<head>
<title>A Comment Test</title>
</head>
<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page
source --%>
</body>
</html>
The above code will generate the following result −

A Test of Comments

Syntax & Purpose


1 <%-- comment --%>
A JSP comment. Ignored by the JSP engine.
2 <!-- comment -->
An HTML comment. Ignored by the browser.
3 <\%
Represents static <% literal.
4 %\>
Represents static %> literal.
5 \'
A single quote in an attribute that uses single quotes.
6 \"
A double quote in an attribute that uses double quotes.

JSP Elements
There are three types of JSP elements present:
1. Directive

2. Action

3. Scripting

1. Directive Elements

(a) page: <%@ page … %>


● Defines page-dependent attributes, such as session tracking,
error page, and buffering requirements.
● is used for importing a package.
● is used for Handling an Exception.

Attributes that we can use are:


● language=”Scripting language” : is used to set which type of
language it is.
● import=”import List” : is used to import the packages,and so
on…
● session=”true/false”
● contentType=”ctinfo”
● errorPage=”error-url”
● isErrorPage=”true/false”
● info=”information”
● isELIgnored=”true/false”
● isThreadSafe=”true/false”

In all the above “import” attribute can be used many times.


Example:
<%@ page language="java" import="java.util.*,java.sql.*" info="Contact
page"
extends="com.rajkumar.User" contextType="text/html"
isELIgnored="false" isThreadSafe="false" session="false" %>
(b) include: <%@ include … %>
● Includes a file during the translation phase
● for using other JSP pages in the current JSP page

Example:
<%@ include file="filename" %>
<%@ include file="header.jsp" %>

(c) taglib: <%@ taglib … %>


● Declares a tag library, containing custom actions, that is used on
the page for using Spring Frameworks
● Template Text
● Template data refers to the static HTML or XML content of the JSP.
Aside from the usual substitutions, such as those based on quoting
and escape sequences, the template data is written verbatim as
part of the JSP response.

2. Scripting Element

Scriptlet elements must be written within the <% … %> tags.The


Scriptlet tag allows writing Java code statements within the JSP
page.The tag is responsible for implementing the functionality of
“_jspService()” by scripting the java code.The JSP engine will
process any code written within the pair of <% and %> tags and
any other code is treated as plain text while translating the JSP
page. There are three main subdivisions of Scripting Elements in
Java Server Pages.
● Expression Tag
● Scriptlet Tag
● Declaration Tag

Scriptlet tag
This tag allows users to insert java code in JSP. The statement
which is written will be moved to _jspservice() using a JSP
container while generating a servlet from JSP. When a client makes
a request, JSP service method is invoked and after that the
content which is written inside the scriptlet tag executes.
<html>
<body>
<% out.print("GeeksforGeeks"); %> <!-- scriptlet tag -->
</body>
</html>

Explanation
● The Syntax of JSP Scriptlet tag is begin with ”.
● We can write our java code inside this tag.
● In java we use System.out.println for printing anything on
console. In JSP, we use only out.print to write something on
console because the out we’re referring to isn’t System.out,
it’s a variable in the effective method that wraps our JSP page.
● System.out writes to the servlet container’s console (usually a
log file); out is a different class entirely which writes to the
output stream for the generated response.

Expression tag is one of the scripting elements in JSP. Expression


Tag in JSP is used for writing your content on the client-side. We
can use this tag for displaying information on the client’s
browser. The JSP Expression tag transforms the code into an
expression statement that converts into a value in the form of a
string object and inserts into the implicit output object.
Syntax:
JSP tag
<%= expression %>

Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>GeeksforGeeks</title>
</head>

<body>
<% out.println("Hello Geeks "); %> <!-- Sriptlet Tag-->
<% int n1=10; int n2=30; %><!-- Sriptlet Tag-->
<% out.println("<br>sum of n1 and n2 is "); %> <!-- Sriptlet Tag-->
<%= n1+n2 %> <!-- Expression tag -->
</body>

</html>

3. Declaration tag
Declaration tag is one of the scripting elements in JSP.
This Tag is used for declare the variables. Along with this,
Declaration Tag can also declare method and classes. Jsp initializer
scans the code and find the declaration tag and initializes all the
variables, methods, and classes. JSP container keeps this code outside
of the service method (_JSPService()) to make them class level
variables and methods.
Syntax of JSP-Declaration Tag

<%! inside this tag we can initialise


our variables, methods and classes %>

Example of JSP Declaration Tag

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-
1">
<title>GeeksforGeeks</title>
</head>

<body>
<!--declaration of username variable.... -->
<%! String username="Geeks"; %>

<!--In expression tag a string is initialised as Geeks -->


<%="Hello : "+username %>

<!-- Displaying expression using Expression Tag -->


</body>
</html>

Difference between the JSP Expression, Declarative, and


Scriptlet tags
● Expression tag: This tag contains a scripting language
expression that is 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 text in a JSP file. You cannot use a
semicolon to end an expression.
● Declaration tag: This declares one or more variables or
methods for use later in the JSP source file. It must
contain at least one complete statement. You can declare
any number of variables or methods within one declaration
tag, but you have to separate them by semicolons. The
declaration must be valid in the scripting language used in
the JSP file.You can add method to the declaration part.
● Scriptlet tag: You can declare variables in the script-let
and can do any processing. All the Scriptlet go to the
inside service() method of the convert servlet.

You might also like