0% found this document useful (0 votes)
21 views18 pages

Java Server Pages: Dr. Shreyas Simu Asst. Prof. ECS Dept. DBCE - Fatorda

Uploaded by

cloyrodrigues06
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)
21 views18 pages

Java Server Pages: Dr. Shreyas Simu Asst. Prof. ECS Dept. DBCE - Fatorda

Uploaded by

cloyrodrigues06
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/ 18

Dr.

Shreyas Simu

JAVA SERVER PAGES Asst. Prof.


ECS Dept.
DBCE - Fatorda
AGENDA/CONTENTS
What is JSP?
JSP Tags
Tomcat
Request String
User Sessions
Cookies
Session Objects
Servlet JSP
Servlet is a java code. JSP is a HTML-based compilation code.
Writing code for servlet is harder than JSP as

WHAT IS JSP?
JSP is easy to code as it is java in HTML.
it is HTML in java.
JSP is slower than Servlet because the first
Servlet is faster than JSP. step in the JSP lifecycle is the translation of
JSP to java code and then compile.

Java ServerPage (JSP) Servlet can accept all protocol requests. JSP only accepts HTTP requests.
is a server-side program In Servlet, we can override the service()
In JSP, we cannot override its service() method.
that is similar in design method.
and functionality to a In Servlet by default session management is In JSP session management is automatically
Java servlet. not enabled, user have to enable it explicitly. enabled.

A JSP is called by a Packages can be imported into the JSP


Packages are to be imported on the top of
program (i.e, bottom , middleclient-side, or top
client to provide a web the program.
)
service, the nature of
It cannot handle extensive data processing
which depends on the It can handle extensive data processing.
very efficiently.
J2EE application.
The facility of writing custom tags is not
The facility of writing custom tags is present.
present.
Before the execution, JSP is compiled in Java
Servlets are hosted and executed on Web
Servlets and then it has a similar lifecycle as
Servers.
Servlets.
WHAT IS JSP?
A JSP is simpler to create than a Java servlet because a JSP is written in HTML rather than with the Java
programming language.
This means that the JSP isn't cluttered with many println() methods as found in a Java servlet. However, a JSP
offers basically the same features found in a Java servlet because a JSP is converted to a Java servlet the first
time that client requests the JSP.
There are three methods that are automatically called when a JSP is requested and when the JSP terminates
normally. These are the jsplnt() method, the jspDestroy() method, and the service() method.
The jsplnt() method is identical the init() method in a Java servlet and in an applet. The jsplnt() method is called
first when the JSP is requested and is used to initialize objects and variables that are used throughout the life
of the JSP.
The jspDestroy() method is identical to the destroy() method in a Java servlet. The destroy() method is
automatically called when the JSP terminates normally. It isn't called if the JSP abruptly terminates such as
when the server crashes. The destroy() method is used for cleanup where resources used during the execution of
the JSP are released, such as disconnecting from a database.
The service() method is automatically called and retrieves a connection to HTTP.
INSTALLATION
Once a JSP is created, you place the JSP in the same directory as HTML pages.
This differs from a Java servlet, which must be placed in a particular directory that is
included in the CLASSPATH.
You don't need to set the CLASSPATH to reference a JSP.
However, there are three factors that you must address when installing a JSP.
First, web servjces called by a JSP must be installed properly.
The second factor to be addressed is to avoid placing the JSP in the WEB-INF or META-INF
directories. The development environment prohibits this.
The last factor is that the directory name used to store a JSP mustn't have the same name
as the prefix of the URL of the web application.
JSP TAGS
JSP program consists of a combination of HTML tags and JSP tags. JSP tags define Java code that i to be
executed before the output of the JSP program is sent to the browser.
A JSP tag begins with a <%, which is followed by Java code, and ends with %>.
There is also an Extendable Markup Language (XML) version of JSP tags, which are formatted as
<jsp:TagID> </ JSP:TagID>.
JSP tags are embedded into the HTML component of a JSP program and are processed by a JSP virtual
engine such as Tomcat.
Tomcat reads the JSP program whenever the program is called by a browser and resolves JSP tags, then
sends the HTML tags and related information to the browser.
Java code associated with JSP tags in the JSP program is executed when encountered by Tomcat, and the
result of that process is sent to the browser.
The browser knows how to display the result because the JSP tag is enclosed within an open and closed
HTML tag.
TYPES OF JSP TAGS
Comment tag: A comment tag opens with<%-- and closes with--%>, and is followed by a
comment that usually describes the functionality of statements that follow the comment tag.
Declaration statement tags: A declaration statement tag opens with <%! and is followed by
a Java declaration statement(s) that define variables, objects, and methods that are available
to other components of the JSP program .
Directive tags A directive tag opens with<%@ and commands the JSP virtual engine to
perform a specific task, such as importing a Java package required by objects and methods
used in a declaration statement.
The directive tag closes with %>.
TYPES OF JSP TAGS
There are three commonly used directives. TI1ese are import, include, and taglib.
The import tag is used to import Java packages into the JSP program.
The include tag inserts a specified file into the JSP program replacing the include tag.
The taglib tag specifies a file that contains a tag library.
<%@ page import=" import java.sql.* "; %>
<%@ include ftle="keogh\books.html" %>
<%@ taglib uri= "rnyTags.tld" %>
Expression tags An expression tag opens with <%= and is used for an expression statement whose
result replaces the expression tag when the JSP virtual engine resolves JSP tags. An expression tags
closes with%> .
Scriptlet tags A scriptlet tag opens with <% and contains commonly used Java control statements and
loops. A scriptlet tag closes with%>.
VARIABLES AND OBJECTS
You can declare Java variables and objects that are used in a JSP program by using the same
coding technique as used to declare them in Java.
However, the declaration statement must appear as a JSP tag within the JSP program before the
variable or object is used in the program.
We will see an example of the same. In this example, the program declares an int called age and
initializes the variable with the number 29.
The declaration statement is placed within JSP tag. You'll notice that this JSP tag begins with<%!.
This tells the JSP virtual engine to make statements contained in the tag available to other JSP tags in
the program.
The variable age is used in an expression tag that is embedded within the HTML paragraph tag
<P>. A JSP expression tag begins with <%=, which is followed by the expression.
The JSP virtual engine resolves the JSP expression before sending the output of the JSP program to
the browser. That is, the JSP tag <%=age%> is replaced with the number 29; afterwards, the HTML
paragraph tag and related information is sent to the browser.
<HTML>
Below shows how to declare an object and an array.
<HEAD>
The JSP program creates three String objects, the first two
<TITLE> JSP Programming </TITLE>
declarations implicitly allocate memory, and the third declaration
</HEAD>
explicitly allocates memory.
<BODY>
Ln addition, this JSP program creates arrays and a Vector.
<%! int age=29; %>
<P> Your age is: <%=age%> </P>
<HTML>
</BODY>
<HEAD>
< / HTML>
<TITLE> JSP Programming </ TITLE>
</ HEAD>
You are able to place multiple statement within
<BODY>
a JSP tag by extending the close JSP tag to another
<%! String Name;
line in the JSP program.
String[ ] Telephone= { "201-555-1212 ", "201-555-4433 " } ;
String Company = new String () ;
<HTML>
Vector Assignments= new Vector() ;
<HEAD>
int[] Grade= {100, 82 ,93};
<TITLE> JSP Programming < / TITLE>
%>
</HEAD>
</ BODY>
<BODY>
</ HTML>
<%! int age=29;
float salary;
int empnurnber ;
%>
</ BODY>
</ HTML>
METHODS
JSP offers the same versatility that you have with Java programs, a method is defined
similar to how a method is defined in a Java program except the method definition is
placed within a JSP tag.
<HTML>
<HEAD>
<TITLE> JSP Programming </TITLE>
</HEAD>

CONTROL STATEMENTS <BODY>


<% ! int grade=70;%>
<% if (grade > 69) { %>
<P> You passed! </ P>
There are two control statements used to <%} else { %>
change the flow of a JSP program. <P> Better luck next time. </P>
<%} %>
These are the if statement and the switch <% switch (grade) {
statement, both of which are also used to case 90 : %>
<P> Your final grade is a A </P>
direct the flow of a Java program. <% break; %>
case 80 : %>
The if statement evaluates a condition <P> Your final grade is a B </P>
statement to determine if one or more <% break;
lines of code are to be executed or case 70 : %>
skipped. <P> Your final grade is a C </P>
<% break;
Similarly, a switch statement compares a case 60 : %>
<P> Your final grade is an F </P>
value with one or more other values <% break ;
associated with a case statement. }
%>
</ BODY>
</HTML>
LOOPS
JSP loops are nearly identical to loops that you use in your Java program except you
can repeat HTML tags and related information multiple times within your JSP program
without having to enter the additional HTML tags.
There are three kinds of loops commonly used in a JSP program. These are the for
loop, the while loop, and the do ... while loop.
Here's what you need to do to download and install
Tomcat:
1. Connect to jakarta.apache.org.

TOMCAT 2. Select Download.


3. Select Binaries to display the Binary Download page.
4. Create a folder from the root directory called tomcat.
5. Download the latest release of jakarta-tomcat.zip to the
JSP programs are executed by a JSP tomcat folder.
Virtual Machine that runs on a web server.
6. Unzip jakarta-tomcat.zip. You can download a demo
Therefore, you'll need to have access to a
JSP Virtual Machine to run your JSP copy of WinZip from www:winzip.com if you don't have a
program. zip/unzip program installed on your computer.
7. The extraction process should create the following
One of the most popular JSP Virtual folders in the tomcat directory: bin, conf, doc, lib src, and
Machines is Tomcat, and it is downloadable webapps .
at no charge from the Apache web site. 8. Use a text editor such as Notepad and edit the
JAVA_HOME variable in the tomcat.bat file, which is
You'll also need to have the Java located in the \tomcat\bin folder. Make sure the
Development Kit (JDK) installed on your JAVA_HOME variable is assigned the path where the JDK is
computer, which you probably installed installed on your computer.
when you learned Java programming. You 9. Open a DOS window and type \tomcat\bin\tomcat to
can download the JDK at no charge from start Tomcat.
the www.sun.com web site. 10. Open your browser. Enter http://lQcalhost:8080. The
Tomcat home page is displayed on the screen verifying that
Tomcat is running.
REQUEST STRING
Refer the text book.
USER SESSIONS
A JSP program must be able to track a session as a client moves between HTML pages and
JSP programs. There are three commonly used methods to track a session. These are by using a
hidden field, by using a cookie, or by using a JavaBean.
A hidden field is a field in an HTML form whose value isn't displayed on the HTML page. You
can assign a value to a hidden field in a JSP program before the program sends the dynamic
HTML page to the browser.
When the person selects the Submit button on the new HTML page, the user ID stored in the
hidden field and information in other fields on the form are sent by the browser to another JSP
program for processing.
This cycle continues where the JSP program processing the request string receives the user ID as
a parameter and then passes the user ID to the next dynamically built HTML page as a hidden
field. In this way, each HTML page and subsequent JSP program has access to the user ID and
therefore can track the session.
COOKIES
A cookie is a small piece of information created by a JSP program that is stored on the
client's hard disk by the browser.
Cookies are used to store various kinds of information, such as user preferences and an ID
that tracks a session with a JSP database system.
You can create and read a cookie by using methods of the Cookie class and the response
object.
Kindly refer TB (J2EE) for the code and its explanation.
SESSION OBJECTS
A JSP database system is able to share information among JSP programs within a session by
using a session object.
Each time a session is created, a unique ID is assigned to the session and stored as a cookie.
The unique ID enables JSP programs to track multiple sessions simultaneously while
maintaining data integrity of each session.
The session ID is used to prevent the intermingling of information from clients.
In addition to the session ID, a session object is also used to store other types of information,
called attributes. An attribute can be login infonnation, preferences, or even purchases placed
in an electronic shopping cart.
Kindly refer TB (J2EE) for the code and its explanation.

You might also like