0% found this document useful (0 votes)
22 views

Java Servlet

Uploaded by

jemalmani22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Servlet

Uploaded by

jemalmani22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

What is a Servlet?

 Servlet is a technology i.e. used to create web application.


 Servlet is an API that provides many interfaces and classes including
documentations.
 Servlet is an interface that must be implemented for creating any
servlet.
 Servlet is a class that extend the capabilities of the servers and
respond to the incoming request.
 Servlet is a web component that is deployed on the server to create
dynamic web page.
Cont.
Servlet API

The javax.servlet and javax.servlet.http packages represent interfaces


and classes for servlet API.
The javax.servlet package contains many interfaces and classes that
are used by the servlet or web container.
The javax.servlet.http package contains interfaces and classes that are
responsible for http requests only.
Life Cycle of a Servlet (Servlet Life Cycle)


Servlet class is loaded.

Servlet instance is created.

init method is invoked.

service method is invoked.

destroy method is invoked.
Servlet Interface

Servlet interface needs to be implemented for creating any
servlet (either directly or indirectly).

It provides 3 life cycle methods that are used to initialize
the servlet, to service the requests, and to destroy.
GenericServlet class

GenericServlet class implements Servlet, ServletConfig,
and Serializable interface.

It provides the implementation of all the methods of these
interfaces except the service method.

GenericServlet class can handle any type of request so it is
protocol-independent.
HttpServlet class

The HttpServlet class extends the GenericServlet class and
implements Serializable interface.

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
information's, attributes etc.
Example of ServletRequest
Index.html

<form action="welcome" method="get">


Enter your name<input type="text" name="name"><br>
<input type="submit" value="login">
</form>
Cont.
Welcome.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServ extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");//will return value
pw.println("Welcome "+name);
pw.close();
}}
RequestDispatcher in Servlet

• The RequestDispatcher interface provides the facility of


dispatching the request to another resource it may be html,
servlet or jsp.
• This interface can also be used to include the content of
another resource also.
• It is one of the way of servlet collaboration.
• There are two methods defined in the RequestDispatcher
interface.
Cont.

Syntax:
RequestDispatcher rd=request.getRequestDispatcher("servlet2");
rd.forward(request, response);//method may be include or forward
Or
rd.include(request, response);
Cont.
Cookies in Servlet

A cookie is a small piece of information that is persisted


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.
Cont.
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 signout.
Cont.

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.
How to create Cookie?

Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object


response.addCookie(ck);//adding cookie in the response
How to delete Cookie?

Cookie ck=new Cookie("user","");//deleting value of cookie


ck.setMaxAge(0);//changing the maximum age to 0 seconds
response.addCookie(ck);//adding cookie in the response
How to get Cookies?
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++){
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
}
URL Rewriting

In URL rewriting, we append a token or
identifier to the URL of the next Servlet or the
next resource.

We can send parameter name/value pairs using
the following format:

url?name1=value1&name2=value2&??
Cont.

Advantage of URL Rewriting

It will always work whether cookie is disabled or
not (browser independent).

Extra form submission is not required on each
pages.

Disadvantage of URL Rewriting

It will work only with links.

It can send Only textual information.

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:

bind objects

view and manipulate information about a
session, such as the session identifier, creation
time, and last accessed time.

You might also like