Course: Java Programming
(CET2031B)
SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY
Java Programming 1
Lab 5
Problem Statement
Develop a simple web application using the concept of Servlets, JSP, and database
connectivity
Java Programming 2
Objective of the Assignment
• To understand server-side scripting
• To understand the concept of Servlet and JSP
• To learn the working of Servlet and JSP
• To learn the database connectivity using MySQL
• To develop web application using Servlet, JSP and database
connectivity
Java Programming 3
JSP
JSP technology is used to create web application just like Servlet technology but with
more functionality
A JSP page consists of HTML tags and JSP tags.
Advantages of JSP over Servlet
• Extension to Servlet
• Easy to maintain - we can easily separate our business logic from presentation logic
• Fast Development: No need to recompile and redeploy
• Less code than Servlet
JSP page is translated into Servlet by the help of JSP translator. The JSP translator
is a part of the web server which is responsible for translating the JSP page into
Servlet. After that, Servlet page is compiled by the compiler and gets converted
into the class file.
Java Programming 4
Creating a simple JSP Page
• To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have
saved this file as index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache
tomcat to run the JSP page.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
How to run a simple JSP Page?
Follow the following steps to execute this JSP page:
• Start the server
• Put the JSP file in a folder and deploy on the server
• Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example,
http://localhost:8888/myapplication/index.jsp
Java Programming 5
Directory Structure of JSP
The directory structure of JSP page is same as Servlet. We contain the JSP page
outside the WEB-INF folder or in any directory.
Java Programming 6
JSP API
The JSP API consists of two packages:
1. javax.servlet.jsp
2. javax.servlet.jsp.tagext
The javax.servlet.jsp package has two interfaces and classes.The two
interfaces are as follows:
1. JspPage
2. HttpJspPage
The classes are as follows:
1. JspWriter 2. PageContext
3. JspFactory 4. JspEngineInfo
5. JspException 6. JspError
Methods of JspPage interface
1. public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP
page is requested firstly. It is used to perform initialization. It is same as the init()
method of Servlet interface.
2. public void jspDestroy(): It is invoked only once during the life cycle of the JSP before
the JSP page is destroyed. It can be used to perform some clean up operation.
Method of HttpJspPage interface:
1. public void _jspService(): It is invoked each time when request for the JSP page comes
to the container. It is used to process the request. The underscore _ signifies that you
cannot override this method.
Java Programming 7
Creating JSP in Eclipse IDE with Tomcat
server
1. Create the dynamic web project
For creating a dynamic web project click <%@ page language="java" contentType="text/html;
on File Menu -> New -> dynamic web charset=ISO-8859-1"
project -> write your project name e.g. first pageEncoding="ISO-8859-1"%>
-> Finish. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
2. Create the JSP file in eclipse IDE "http://www.w3.org/TR/html4/loose.dtd">
For creating a JSP file, explore the project <html>
by clicking the + icon -> right click on <head>
WebContent -> New -> jsp -> write your <meta http-equiv="Content-Type" content="text/html;
jsp file name e.g. index -> next -> Finish. charset=ISO-8859-1">
After creating JSP file, write the code. <title>JSP - Hello World </title>
</head>
3. Start the tomcat server and deploy <body>
the project <%= "Hello World!" %>
For starting the server and deploying the </body>
project in one step Right click on your </html>
project -> Run As -> Run on Server ->
choose tomcat server -> next -> addAll ->
finish.
Java Programming 8
Thank You!!
Java Programming 9