0% found this document useful (0 votes)
14 views13 pages

Web Technology Practical No. 6

The document outlines an assignment to implement a sample program using JavaServer Pages (JSP) for web application development, detailing objectives, software requirements, and the theory behind JSP. It explains the advantages of JSP over other technologies, the architecture and processing steps involved in JSP, as well as its syntax, including scriptlets, declarations, expressions, and comments. Additionally, it provides a sample JSP program that connects to a database and retrieves student information, along with a FAQ section addressing common questions about JSP.

Uploaded by

nilimapawase14
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)
14 views13 pages

Web Technology Practical No. 6

The document outlines an assignment to implement a sample program using JavaServer Pages (JSP) for web application development, detailing objectives, software requirements, and the theory behind JSP. It explains the advantages of JSP over other technologies, the architecture and processing steps involved in JSP, as well as its syntax, including scriptlets, declarations, expressions, and comments. Additionally, it provides a sample JSP program that connects to a database and retrieves student information, along with a FAQ section addressing common questions about JSP.

Uploaded by

nilimapawase14
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/ 13

Assignment No: 6

Problem Statement: Implement the sample program demonstrating the use of JSP.
Objectives: To use client side and server side web technologies.
Outcome: Apply the client side and server side technologies for web application
development
Software & Hardware Requirments:
Operating System: Ubuntu
Software: Eclipse
Programming Language: Java(Servlet/JSP)
Server: Tomcat
Browser: Mozilla/Google Chrome
Hardware: i3 Processor, 4GB RAM, 500GB HDD

Theory:
1. JavaServer Pages.
JavaServer Pages (JSP) is a technology for developing web pages that support dynamic
content which helps developers insert java code in HTML pages by making use of special JSP
tags, most of which start with <% and end with %>.
A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role
of a user interface for a Java web application. Web developers write JSPs as text files that
combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.
Using JSP, you can collect input from users through web page forms, present records
from a database or another source, and create web pages dynamically.
JSP tags can be used for a variety of purposes, such as retrieving information from a
database or registering user preferences, accessing JavaBeans components, passing control
between pages and sharing information between requests, pages etc.

1.1 Why Use JSP?


JavaServer Pages often serve the same purpose as programs implemented using the
Common Gateway Interface (CGI). But JSP offer several advantages in comparison with the
CGI.
● Performance is significantly better because JSP allows embedding Dynamic Elements in
HTML Pages itself instead of having a separate CGI files.
● JSP are always compiled before it's 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.

1.2 Advantages of JSP:


Following is the list of other advantages of using JSP over other technologies:
● vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic
part is written in Java, not Visual Basic or other MS specific language, so it is more
powerful and easier to use. Second, it is portable to other operating systems and non-
Microsoft Web servers.
● vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to
have plenty of println statements that generate the HTML.
● vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for
"real" programs that use form data, make database connections, and the like.
● vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly
interact with the web server to perform complex tasks like database access and image
processing etc.
● vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

1.3 JSP - Architecture


The web server needs a JSP engine ie. container to process JSP pages. The JSP container
is responsible for intercepting requests for JSP pages. This tutorial makes use of Apache which
has built-in JSP container to support JSP pages development.
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 web page 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 that 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, which the servlet
engine passes to the web server inside an HTTP response.
● The web server forwards the HTTP response to your browser in terms of static HTML
content.
● Finally 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 shown below in the following diagram:

1.4. 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.

1.4.1 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...

Typically initialization is performed only once and as with the servlet init method, you
generally initialize database connections, open files, and create lookup tables in the jspInit
method.
1.4.2 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...

The _jspService() method of a JSP is invoked once per a request and is responsible for
generating the response for that request and this method is also responsible for generating
responses to all seven of the HTTP methods ie. GET, POST, DELETE etc.

1.4.3 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.

1.5. JSP – Syntax


1.5.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.
Following is the syntax of Scriptlet:
<% code fragment %>

You can write XML equivalent of the above syntax as follows:


<jsp:scriptlet>
code fragment
</jsp:scriptlet>
Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following
is the simple and first example for JSP:
<html>

<head><title>Hello World</title></head>

<body>

Hello World!<br/>

<%

out.println("Your IP address is " + request.getRemoteAddr());

%>

</body>

</html>

1.5.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.
Following is the syntax of JSP Declarations:
<%! declaration; [ declaration; ]+ ... %>
You can write XML equivalent of the above syntax as follows:
<jsp:declaration>
code fragment
</jsp:declaration>

Following is the simple example for JSP Declarations:


<%! int i = 0; %>

<%! int a, b, c; %>

<%! Circle a = new Circle(2.0); %>

1.5.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.
Following is the syntax of JSP Expression:
<%= expression %>
You can write XML equivalent of the above syntax as follows:
<jsp:expression>
expression
</jsp:expression>

Following is the simple example for JSP Expression:


<html>

<head><title>A Comment Test</title></head>

<body>

<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>

</body>

</html>

1.5.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" part of your JSP page.
Following is the syntax of JSP comments:
<%-- This is JSP comment --%>

Following is the simple example for JSP Comments:

1.6. JSP Directives:


A JSP directive affects the overall structure of the servlet class. It usually has the
following form:
<%@ directive attribute="value" %>

There are three types of directive tag:


Directive Description
Defines page-dependent attributes, such as scripting language,
<%@ page ... %>
error page, and buffering requirements.
<%@ include ... %> Includes a file during the translation phase.
Declares a tag library, containing custom actions, used in the
<%@ taglib ... %>
page

1.7. JSP Implicit Objects:


JSP supports nine automatically defined variables, which are also called implicit objects.
These variables are:
Objects Description
request This is the HttpServletRequest object associated with the
request.
This is the HttpServletResponse object associated with the
response
response to the client.
out This is the PrintWriter object used to send output to the client.
session This is the HttpSession object associated with the request.
This is the ServletContext object associated with application
application
context.
config This is the ServletConfig object associated with the page.
This encapsulates use of server-specific features like higher
pageContext
performance JspWriters.
This is simply a synonym for this, and is used to call the
page
methods defined by the translated servlet class.
The Exception object allows the exception data to be accessed
Exception
by designated JSP.

Conclusion:
Hence, we have successfully designed web page to demonstrate the use of JSP.
.
Program
showinfo.jsp

<%@pageimport="java.sql.*"language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.print("<h2>Student Information Table</h2>");
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from studentinfo");
out.print("<center><table border=\"1\">");
out.print("<tr><th>Id</th><th>Student
Name</th><th>Class</th><th>Division</th><th>City</th></tr>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rs.getString(1)+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(5)+"</td>");
out.print("</tr>");
}
out.print("</table></center>");
}
catch(Exception e)
{
e.printStackTrace();
}

%>
</body>
</html>
DataBase:

OutPut:
FAQs:
1. What is JSP?
2. What is the syntax of JSP?
3. How do we connect JSP file to database?
4. What is session management?
5. Explain advantages of JSP over Servlet?
6. Explain Scriptlet, Expression, Declaration tag in JSP?
7. List default object in JSP?

You might also like