Servlet Notes
Servlet Notes
Servlet Notes
HTTP is a protocol that clients and servers use on the web to communicate.
HTTP is a stateless protocol i.e HTTP supports only one request per connection.
This means that with HTTP the clients connect to the server to send one request and
then disconnects. This mechanism allows more users to connect to a given server
over a period of time.
The client sends an HTTP request and the server answers with an HTML page to the
client, using HTTP.
HTTP Methods
HTTP request can be made using a variety of methods, but the ones you will use most
often are Get and Post. The method name tells the server the kind of request that
is being made, and how the rest of the message will be formated.
POST Request for server to accept the entity enclosed in the body of HTTP method.
URL SYNTEX:
Servlet Technology is used to create web applications. Servlet technology uses Java
language to create web applications
Web applications are helper applications that resides at web server and build
dynamic web pages. A dynamic page could be anything like a page that randomly
chooses picture to display or even a page that displays the current time.
HTTPSERVLET ::
The service() method of HttpServlet class listens to the Http methods (GET, POST
etc) from request stream and invokes doGet() or doPost() methods based on Http
Method type.
Cookies getCookies() returns an array containing all of the Cookie objects the
client sent with this request
PrintWriter getWriter() returns a PrintWriter object that can send character text
to the client.
request
session
application
Session : We all know that HTTP is a stateless protocol. All requests and responses
are independent. But sometimes you need to keep track of client's activity across
multiple requests. For eg. When a User logs into your website, not matter on which
web page he visits after logging in, his credentials will be with the server, until
he logs out. So this is managed by creating a session.
There are four different techniques used by Servlet application for session
management. They are as follows:
Cookies
Hidden form field
URL Rewriting
HttpSession
session.setAttribute("karan",n);
data ne bija servlet page ma lai java mate response.sendRedirect("url mapping
name");use thay or requestDispatcher
ana sivay out.print("<a href='servlet2'>visit</a>"); url mappkaryu
Cookies
Cookies are small pieces of information that are sent in response from the web
server to the client. Cookies are the simplest technique used for storing client
state.
Cookies are stored on client's computer. They have a lifespan and are destroyed by
the client browser at the end of that lifespan.
Cookies are created using Cookie class present in Servlet API. Cookies are added to
response object using the addCookie() method. This method sends cookie information
over the HTTP response stream. getCookies() method is used to access the cookies
that are added to response object.
response.addCookie(ck);
Cookie[] ks=request.getCookies();
Types of Cookies
There are two types of cookies. They are as following:
Session
Persistent
1) Session cookies:
The session cookies do not have any expiration time. It is present in the browser
memory. When the web browser is closed then the cookies are destroyed
automatically.
2) Persistent Cookies:
The Persistent cookies have an expiration time. It is stored in the hard drive of
the user and it is destroyed based on the expiry time.
URL REWRITING :
response.sendRedirect("First?user_name="+ name);