You are on page 1of 48

Chapter 3

Servlets
What is a Servlet?
Servlet can be described in many ways, depending on the context.
•Servlet is a technology which is used to create a web application.
•Servlet is an API that provides many interfaces and classes including documentation.
•Servlet is an interface that must be implemented for creating any Servlet.
•Servlet is a class that extends the capabilities of the servers and responds to the incoming
requests. It can respond to any requests.
•Servlet is a web component that is deployed on the server to create a dynamic web page
Execution of Servlets :
Execution of Servlets involves six basic steps:
1.The clients send the request to the web server.
2.The web server receives the request.
3.The web server passes the request to the corresponding servlet.
4.The servlet processes the request and generates the response in the form of output.
5.The servlet sends the response back to the web server.
6.The web server sends the response back to the client and the client browser displays it on the screen.

Servlet Architecture
The diagram shows the servlet architecture:
What is a web application?
A web application is an application accessible from the web. A web application is composed of
web components like Servlet, JSP, Filter, etc. and other elements such as HTML, CSS, and
JavaScript. The web components typically execute in Web Server and respond to the HTTP
request.
CGI (Common Gateway Interface)
CGI technology enables the web server to call an external program and pass HTTP request
information to the external program to process the request. For each request, it starts a new
process.
Disadvantages of CGI
There are many problems in CGI technology:
1.If the number of clients increases, it takes more time for sending the response.
2.For each request, it starts a process, and the web server is limited to start processes.
3.It uses platform dependent language e.g. C, C++.
Advantages of Servlet
There are many advantages of
Servlet over CGI. The web
container creates threads for
handling the multiple requests to
the Servlet. Threads have many
benefits over the Processes such as
they share a common memory
area, lightweight, cost of
communication between the
The advantages of Servlet are as follows: threads are low
1.Better performance: because it creates a thread for each request, not process.
2.Portability: because it uses Java language.
3.Robust: JVM manages Servlets, so we don't need to worry about the memory leak, 
garbage collection, etc.
4.Secure: because it uses java language.
Servlets API’s:
•javax.servlet(Basic)
Servlets are build from two packages:
•javax.servlet.http(Advance)
Various classes and interfaces present in these packages are
Component Type Package
Servlet Interface javax.servlet.*

ServletRequest Interface javax.servlet.*

ServletResponse Interface javax.servlet.*

GenericServlet Class javax.servlet.*

HttpServlet Class javax.servlet.http.*

HttpServletRequest Interface javax.servlet.http.*

HttpServletResponse Interface javax.servlet.http.*

Filter Interface javax.servlet.*

ServletConfig Interface javax.servlet.*


Life Cycle of a Servlet (Servlet Life Cycle)
The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet:
1.Servlet class is loaded.
2.Servlet instance is created.
3.init method is invoked.
4.service method is invoked.
5.destroy method is invoked.

Init() destroy()
Servlet is in service
Servlet to create

As displayed in the above diagram, there are three


Invoke many
states of a servlet: new(init), ready(service) and times
end(destroy). The servlet is in new state if servlet Service()
instance is created. After invoking the init() method,
Servlet comes in the ready state. In the ready state,
servlet performs all the tasks. When the web
container invokes the destroy() method, it shifts to
the end state.
1.init method is invoked
• init() method is called only once when servlet is created(after creating object of servlet
class) same like applet. The init method is used to initialize the servlet. It is the life cycle
method of the javax.servlet.Servlet interface.
Syntax of the init method is given below:
public void init(ServletConfig config) throws ServletException  

2.service method is invoked


• It is main method of servlet that performs actual task.
• Whenever request is made by client browser, then servlet container calls the service()
method.
• Work of service() method is to handle requests coming from client.
• Service() method checks the HTTP request type like get,post and calls the doGet(),doPost()
•  Notice that servlet is initialized only once
The syntax of the service method of the Servlet interface is given below:
public void service(ServletRequest request, ServletResponse response)   
  throws ServletException, IOException  
public void doGet(ServletRequest request, ServletResponse response)   throws ServletEx
ception, IOException  
public void doPost(ServletRequest request, ServletResponse response)   throws ServletE
xception, IOException  

3.destroy method is invoked


• The web container calls the destroy method before removing the servlet instance from
the service. (only calls once at the end of servlet life cycle)
• It gives the servlet an opportunity to clean up any resource for example memory, thread
etc
 The syntax of the destroy method of the Servlet interface is given below:

public void destroy()  
{
//cleanup code
}
Types of servlet
servlet must implement the interface from javax.servlet.Servlet. There are 2 main types of servlet
1. Generic servlets
2. HTTP servlets
Generic servlets
• It extends javax.servlet.GenericServlet.
• Generic servlets are protocol independent, means that they contain no inherent support for HTTP
or any other transport protocol.
• Syntax:
class class_nm extends GenericServlet
{ }
HTTP servlets
• These extend javax.servlet.HttpServlet.
• These servlets have built-in support for the HTTP protocol and are much more useful in an
browser environment.
• All servlets extends from HttpServlet rather than from GenericServlet in order to take advantage
of this built-in HTTP support
Syntax : class class_nm extends HttpServlet{ }
 It provides http specific methods such as doGet, doPost, doHead, doTrace etc.
ServletRequest Interface
An object of ServletRequest is used to provide the client request information to a servlet such
as content type, content length, parameter names and values, header informations, attributes
etc.
Method Description
public String getParameter(String name) is used to obtain the value of a parameter by name.
public String[] getParameterValues(String returns an array of String containing all values of given parameter
name) name. It is mainly used to obtain values of a Multi select list box.
getAttribute() Returns the value of Attributes
getProtocol() Return description about Protocol
public String getContentType() Returns the Internet Media Type of the request entity data, or null if
not known.
public ServletInputStream getInputStream() Returns an input stream for reading binary data in the request body.
throws IOException
public abstract String getServerName() Returns the host name of the server that received the request.
public int getServerPort() Returns the port number on which this request was received.
The ServletResponse represents the response which is sent back to the user. The servlet
 container creates a ServletResponse object and passes it as an argument to the servlet’s
service method.
Important Methods of ServletResponse Interface
•public PrintWriter getWriter() : This method returns a PrintWriter object that can send
character text to the client.
•public ServletOutputStream getOutputStream() : This method returns a ServletOutputStream
suitable for writing binary data in the response.
•public void setContentType(String type) : Sets the content type of the response being sent as
response.
Web Terminology
Servlet Terminology Description
Website: static vs dynamic It is a collection of related web pages that may contain text, images, audio and
video.

HTTP It is the data communication protocol used to establish communication


between client and server.
HTTP Requests It is the request send by the computer to a web server that contains all sorts
of potentially interesting information.
Get vs Post It gives the difference between GET and POST request.
Container It is used in java for dynamically generating the web pages on the server side.

Server: Web vs Application It is used to manage the network resources and for running the program or
software that provides services.

Content Type It is HTTP header that provides the description about what are you sending to
the browser.
Steps to create a servlet example
1.Steps to create the servlet using Tomcat server
1. Create a directory structure
2. Create a Servlet
3. Compile the Servlet
4. Create a deployment descriptor
5. Start the server and deploy the application
There are given 6 steps to create a servlet example. These steps are required for all the
servers.
The servlet example can be created by three ways:
2.By implementing Servlet interface,
3.By inheriting GenericServlet class, (or)
4.By inheriting HttpServlet class
The mostly used approach is by extending HttpServlet because it provides http request
specific method such as doGet(), doPost(), doHead() etc.
Execution Process of Servlet Application
Once Tomcat is installed and configured, user can put it to work. Six steps take user from writing
their servlet to running it. These steps are as follows:
Execution Process of Servlet Application
1) Step 1 - Create a Directory Structure under Tomcat:
When you install Tomcat, several subdirectories are automatically created under the Tomcat home directory
(%TOMCAT_HOME%). One of the subdirectories is webapps. The webapps directory is where you store your
web applications. A web application is a collection of servlets and other contents installed under a specific
subset of the server's URL namespace. A separate directory is dedicated for each servlet application.
2) Step 2 - Write the Servlet Source Code:
In this step, you prepare your source code. You can write the source code yourself using your favorite text
editor or copy it from the CD included with the book.
The code in Listing 1.1 shows a simple servlet called TestingServlet. The file, named TestingServlet.java, sends
to the browser a few HTML tags and some text. For now, don't worry if you haven't got a clue about how it
works.
3) Step 3 - Compile Your Source Code:
For your servlet source code to compile, you need to include in your CLASSPATH environment variable the path
to the servlet.jar file. The servlet.jar is located in the common\lib\ subdirectory under %CATALINA_HOME%.
If you are using Windows, remember that the new environment variable takes effect only for new console
windows. In other words, after changing a new environment variable, open a new console window for typing
your command lines.
4) Step 4 - Create the Deployment Descriptor:
A deployment descriptor is an optional component in a servlet application, taking the form of an XML
document called web.xml. The descriptor must be located in the WEB-INF directory of the servlet
application. When present, the deployment descriptor contains configuration settings specific to that
application. Deployment descriptors are discussed in detail in Chapter 16. "Application Deployment."
5) Step 5 - Run Tomcat:
If it is not already running, you need to start Tomcat. For information on how to do that, see Appendix
A, "Tomcat Installation and Configuration."
6) Step 6 - Call Your Servlet from a Web Browser:
You are ready to call your servlet from a web browser. By default, Tomcat runs on port 8080 in myApp
virtual directory under the servlet subdirectory. The servlet that you just wrote is named Testing. The
URL for that servlet has the following format:
http://domain-name/virtual-directory/servlet/servlet-name
Session Tracking in Servlets
Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.
Http protocol is a stateless so we need to maintain state using session tracking techniques.
Each time user requests to the server, server treats the request as the new request. So we need
to maintain the state of an user to recognize to particular user.
HTTP is stateless that means each request is considered as the new request. It is shown in the
figure given below:

Why use Session Tracking?


To recognize the user It is used
to recognize the particular
user.
Session Tracking Techniques
There are four techniques used in Session tracking:
1.Cookies
2.Hidden Form Field
3.URL Rewriting
4. User authorization
5.HttpSession
Cookies
Cookies are the mostly used technology for session tracking. Cookie is a key value pair of
information, sent by the server to the browser. This should be saved by the browser in its space
in the client computer. Whenever the browser sends a request to that server it sends the cookie
along with it. Then the server can identify the client using the cookie.
In java, following is the source code snippet to create a cookie:
Cookie cookie = new Cookie(“userID”, “7456”);
res.addCookie(cookie);
Session tracking is easy to implement and maintain using the cookies. Disadvantage is that, the
users can opt to disable cookies using their browser preferences. In such case, the browser will
not save the cookie at client computer and session tracking fails.
Hidden Fields
<INPUT TYPE=”hidden” NAME=”technology” VALUE=”servlet”>
Hidden fields like the above can be inserted in the webpages and information can be sent to the
server for session tracking. These fields are not visible directly to the user, but can be viewed
using view source option from the browsers. This type doesn’t need any special configuration
from the browser of server and by default available to use for session tracking. This cannot be
used for session tracking when the conversation included static resources like html pages.
URL Rewriting
Original URL: http://server:port/servlet/ServletName
Rewritten URL: http://server:port/servlet/ServletName?sessionid=7456
When a request is made, additional parameter is appended with the url. In general
added additional parameter will be sessionid or sometimes the userid. It will suffice to track
the session. This type of session tracking doesn’t need any special support from the browser.
Disadvantage is, implementing this type of session tracking is tedious. We need to keep
track of the parameter as a chain link until the conversation completes and also should make
sure that, the parameter doesn’t clash with other application parameters.
User authorization
User can be authorized to use the web application in different ways. Basic concept is that the
user will provide username and password to login to the application. Based on that the user can
be identified and the session can be maintained
HttpSession interface
In such case, container creates a session id for each user.The container uses this id to identify
the particular user.An object of HttpSession can be used to perform two tasks:
1.bind objects
2.view and manipulate information about a session, such as the session identifier, creation time,
and last accessed time.
If you want to work with HttpSession Interface ,we have the some steps
1. Create reference object of HttpSession Interface.
2. Use its method to work with HttpSession interface.

1.Create reference object of HttpSession Interface


We have the getSession() method of HttpServletRequest and this method return reference
of HttpSession Interface.
Syntax: New Session
HttpSession session=req.getSession(Boolean);
This method is used to create new session. If we pass true in it
e.g.
HttpSession session=req.getSession(true);
For Existing Session
Syntax:
HttpSession session=req.getSession(); // used for existing session created by user
2. Use its method to work with HttpSession interface.
i. String getId(): return the session Id generated by server for client
ii. Void setAttribute(Object key,Object value):- This method can store the data in session in the
form of key and value pair.
Object Key:- unique identity of session data
Object value:- actual data present in session
iii. Object getAttribute(Object key):- This method is used to return the session data using its
key.If key is not found then it returns null.
iv. Boolen isNew():- Used to check wheather session is new or not. If new session then it returns
true otherwise false.
v. void invalidate():- used to logout the session.
vi. Void setMaxInactiveInterval(int seconds):- can terminate the session forcefully.
vii.public long getCreationTime():Returns the time when this session was created, measured in
milliseconds since midnight January 1, 1970 GMT.
viii.public long getLastAccessedTime():Returns the last time the client sent a request associated
with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
Cookies in Servlet
• A cookie is a small piece of information that is persisted (continue to exist)between the
multiple client requests.
• A cookie has a name, a single value, and optional attributes such as a comment, path and
domain qualifiers, a maximum age, and a version number.
How Cookie works
By default, each request is considered as a new request. In cookies technique, we add
cookie with response from the servlet. So cookie is stored in the cache of the browser. After
that if request is sent by the user, cookie is added with request by default. Thus, we recognize
the user as the old user.
Types of Cookie
There are 2 types of cookies in servlets.
1.Non-persistent cookie
2.Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the browser.
It is removed only if user logout or sign-out.
Advantage of Cookies
1.Simplest technique of maintaining the state.
2.Cookies are maintained at client side.
Disadvantage of Cookies
1.It will not work if cookie is disabled from the browser.
2.Only textual information can be set in Cookie object.
Note: Gmail uses cookie technique for login. If you disable the cookie, gmail won't work.
Cookie class
javax.servlet.http.Cookie class provides the functionality of using cookies. It provides a lot of
useful methods for cookies.
Constructor of Cookie class Constructor Description
Cookie() constructs a cookie.
Cookie(String name, String constructs a cookie with a
value) specified name and value.
Useful Methods of Cookie class
Method Description
public void setMaxAge(int expiry) Sets the maximum age of the cookie in seconds.
public String getName() Returns the name of the cookie. The name
cannot be changed after creation.
public String getValue() Returns the value of the cookie.
public void setName(String name) changes the name of the cookie.
public void setValue(String value) changes the value of the cookie.
For adding cookie or getting the value from the cookie, we need some methods provided by
other interfaces. They are:

• public void addCookie(Cookie ck):method of HttpServletResponse interface is used to add


cookie in response object.
• public Cookie[] getCookies():method of HttpServletRequest interface is used to return all
the cookies from the browser.
How to submit Html page to Servlet
We have few steps
Step1: create the html page.
Step2: design the html form
Step3: create the servlet
Step4: Provide the communication between html page and servlet
Step5: Access the html form data from html to servlet page

For design Html form ,we have following tags


1.<input type=‘text’ name=‘controlname’ value=“/> : used to create textbox using html
2.<input type=‘password’ name=‘controlname’ value=“/> : used to create password
3.<input type=‘checkbox’ name=‘controlname’ value=“/> : used to create checkbox
4.<input type=‘radio’ name=‘controlname’ value=“/> : used to create Redio Button
5.<input type=‘textarea’ name=‘controlname’ value=“/> : used to create textarea or multiline
textbox using html.
6.<input type=‘submit’ name=‘controlname’ value=“/> : used to create submit button
7.<select name=‘controlname’>
<option> value</option> </select>
Every control contain the two major attributes
1. Name :- name indicates the identity of control at server side at the time of data fetching.
2. value:- value indicates, the actual data present in control.

Using name we can access the value of control in servlet


Reading html form/Parameter data
The following methods are used
1. getParameter():-
req.getParameter(); :->used to get value of a form parameter
2.getParameterValues():-checkboxes
Call this method if the parameter appears more than once and return multiple values.
3. getParameterNames():-
Call this method to get complete list of all parameter in correct request.

Step4: Provide the communication between html page and servlet


Communication between html page and servlet, we need form tag.
Syntax:
<form name=“formname” action=“urlname” method=“Get/Post” enctype=“multipart-
fordata/application-x-www-urlencoded”>
</form>
• action:- Indicate where we want to submit the html form data or target servlet url where we
want to submit data.
Method or way of form data submission
1.Get():- if we use the Get() then all html form data visible at browser address bar in the form
of name and value pair.
2. Post():- If we use the Post() then html form data not visible at browser address, send via
internally, body header of page.

Once we provide the communication between html form and servlet, we can access html form
data in servlet.

Access the html form data from html to servlet page


If we want to access html form data in servlet .we have the getParameter() method of
HttpServletRequest and it is present as parameter in doGet() or doPost() in servlet
Syntax:
String value=req.getParameter(“html control name mention in the form tag”)
Servlet JDBC
JSP(Java Server Page)
JSP Introduction
• JSP technology is used to create web application just like Servlet technology.
• JSP is an extension of Servlet because it provides more functionality than servlet such as
expression language, JSTL, 
• A JSP page consists of HTML tags and JSP tags.
• The JSP pages are easier to maintain than Servlet because we can separate designing and
development. It provides some additional features such as Expression Language, Custom Tags,
etc.
Advantages of JSP over Servlet
There are many advantages of JSP over the Servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the Servlet
in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with presentation
logic. In Servlet technology, we mix our business logic with the presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code
needs to be updated and recompiled if we have to change the look and feel of the application.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code.
Moreover, we can use EL, implicit objects, etc.
The Lifecycle of a JSP Page
The JSP pages follow these phases:
•Translation of JSP Page
•Compilation of JSP Page
•Classloading (the classloader loads class file)
•Instantiation (Object of the Generated Servlet is created).
•Initialization ( the container invokes jspInit() method).
•Request processing ( the container invokes _jspService() method).
•Destroy ( the container invokes jspDestroy() method).
Note: jspInit(), _jspService() and jspDestroy() are
the life cycle methods of JSP.
As depicted in the above diagram, 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. Moreover, all the
processes that happen in Servlet are performed on JSP later like
initialization, committing response to the browser and destroy.
The 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.
Elements of JSP
JSP page is built using components such as:
• Declaration tags
• Scriptlets
• Expression
• Directives

• Declaration Tags:
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 initialize 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
e.g.
<%!  field or method declaration %>   <%!
String str=“Hello”;
Note: It is very important to place a semicolon at the end of int a=10; public int sum();
each code placed inside declaration tag. %>
Scriptlet Tag
Scriptlet Tag allows you to write java code inside JSP page. Scriptlet tag implements
the _jspService method functionality by writing script/java code.
Syntax of Scriptlet Tag is as follows :
<html>
<head>
<% JAVA CODE %>
<title>My First JSP Page</title>
</head>
<%! int count = 0;
%>
<body>
Page Count is <% out.println(++count);%>
</body>
</html>
expression tag
The code placed within JSP expression tag is written to the output stream of the
response. So you need not write out.print() to write data. It is mainly used to print the values
of variable or method.
Syntax of JSP expression tag <html>  
<%=  statement %>   <body>  
<%= "welcome to jsp" %>  
</body>  
</html>  

Note: Do not end your statement with semicolon in case of expression tag.
JSP Directives and it’s Attribute
The majore purpose of directives tag is
• Add the page specific attribute on jsp page.
• Include the another page content in jsp page.
• Use the external tag libraries in jsp page
General Syntax:
<%@ attribute subattribute=“value” %>
There are major 3 attribute of directives tag
1. Page attribute:
The page attribute is used for apply the page specific attribute on JSP. This attribute provides
the some sub attributes like as
import
contentType
isErrorPage
errorPage
session
bufferSize
autoFlush etc
• import
The import attribute is used to import class,interface or all the members of a package.It is
similar to import keyword in java class or interface.
<html>  
<body>  
  
<%@ page import="java.util.Date" %>  
Today is: <%= new Date() %>  
  
</body>  
</html>  
• contentType
The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of
the HTTP response.The default value is "text/html;
<html>  
<body>  
<%@ page contentType=application/msword %>  
Today is: <%= new java.util.Date() %>    
</body>  
</html>  
• isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
Note: The exception object can only be used in the error page.
<html>  
<body>  
 
<%@ page isErrorPage="true" %>    
Sorry an exception occured!<br/>  
The exception is: <%= exception %>  
 
</body>  
</html>  
• errorPage
The errorPage attribute is used to define the error page, if exception occurs in the current page,
it will be redirected to the error page.
<html>  
<body>   
<%@ page errorPage="myerrorpage.jsp" %>  
<%= 100/0 %>  
 </body>  
</html>  
2. Include Directive
The include directive is used to include the contents of any resource it may be jsp file, html
file or text file. The include directive includes the original content of the included resource at
page translation time (the jsp page is translated only once so it will be better to include static
resource).

Advantage of Include directive


Code Reusability
Syntax of include directive <%@ include file="resourceName" %>  
<html>   Note: The include directive includes
<body>     the original content, so the actual
<%@ include file="header.html" %>   page size grows at runtime.
  
Today is: <
%= java.util.Calendar.getInstance().getTime() %>  
 
</body>  
</html>  
3.Taglib directive
The JSP taglib directive is used to define a tag library that defines many tags. We use the TLD
(Tag Library Descriptor) file to define the tags. In the custom tag section we will use this tag so it
will be better to learn it in custom tag.
Syntax JSP Taglib directive

<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>  

Example of JSP Taglib directive


In this example, we are using our tag named currentDate. To use this tag we must specify the
taglib directive so the container may get information about the tag.
<html>  
<body>  
<%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>  
  
<mytag:currentDate/>  
 
</body>  
</html>  
we will discuss the Implicit Objects in JSP. These Objects are the Java objects that the JSP
Container makes available to the developers in each page and the developer can call them
directly without being explicitly declared. JSP Implicit Objects are also called pre-defined
variables.
S.No. Object & Description
request
1 This is the HttpServletRequest object associated with the request.
response
2 This is the HttpServletResponse object associated with the response to the client.
out
3 This is the PrintWriter object used to send output to the client.
session
4 This is the HttpSession object associated with the request.
application
5 This is the ServletContext object associated with the application context.
config
6 This is the ServletConfig object associated with the page.
pageContext
7 This encapsulates use of server-specific features like higher performance JspWriters.
8 page
This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
9 Exception
The Exception object allows the exception data to be accessed by designated JSP.

Comment Tag
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", a part of your JSP page.
Following is the syntax of the JSP comments −
<html>     <%-- This is JSP comment --%>
<head>      
<title>
A CommentTest </title>
 </head>  
 <body>       <h2>A Test of Comments</h2>    
  <%-- This comment will not be visible in the page source --%>
  </body>
</html>
Step1 : create HTML page
Step2: create JSP page
Step 3: establish communication between JSP
and HTMl(include form tag in your Html form)

You might also like