0

A servlet is an object that gets data from a post&get and also passes data to the view part of the web application thus can be used as:

  • Process or store data that was submitted from an HTML form
  • Provide dynamic content such as the results of a database query

However in Wikipedia this is also seen:

  • Manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer

So what is mean here by saying filling the articles into the shopping cart of the appropriate customer?

Does this mean a servlet is a Session variable? Can someone explain this behaviour and better yet give an example? Thanks.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • Servlet *manages* variables that were put into HTTP session earlier, thus letting you hold some state while session is alive. In this context one user has one session and thus one set of stateful data. – skuntsel Mar 10 '13 at 18:11
  • 1
    By the way, an extremely enlightening reading is BalusC's answer on [How do servlets work?](http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909). – skuntsel Mar 10 '13 at 18:36

4 Answers4

2

HTTP is stateless. Meaning that the data returned by the server does not depend on any previous actions by the user. Cookies and other non-HTTP methods is what makes the web appear to be stateful, enabling user to for example log-in and out on websites.

Check out: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html

For an application developers, it IS stateless always.

For end users it is appears to be statefull

Now,

Each HTTP request results in a new invocation of a servlet (i.e., a thread calling the servlet’s service and doXxx methods), regardless of whether the connection is keep-alive or not.

EDIT:

HttpSession object is used to set information related to a specific session say, the number of products in a cart for the current session. Note that session gets closed if the browser is closed or if you clear the cookies.

How does the webserver know that its the same session?

Webservers sends a sessionId to the browser in the form of cookie. And, the browser sends the cookie having sessionId back to the server for subsequent requests.

How does the browser identifies which cookies to send for a link/request?

It is based on the these parameters. If the request matches these parameters the browser sends that particular cookie:

Domain: The domain name to which the request is made.

Path: If the context root path name is same.

Secure: Server sends if the given cookie if it can be sent on this non-secure channel

If cookies are disabled then it uses URL-rewriting.

Is it possible to retain the session even after the browser is closed and opened? Yes. The answer is cookie + DB + Googling :)

John Eipe
  • 10,922
  • 24
  • 72
  • 114
  • Sorry, can you elobrate the last part? To retain the state between multiple connections? Isn't cookies because sessions die after a web browser is closed? When you say multiple connections, do you mean connections made after a browser restart? – Koray Tugay Mar 10 '13 at 19:35
0

HttpSession object is accessible from the HttpServletRequest Object passes as method arg in a Servlet's doXXX() method.This session is stateful

0

In java Servlet,

We can store value of a variable from get/post request parameter in

Request State or Session State.

For request state variable -

request.setAttribute("requestvariable", "My Value");

will save the value in a complete request - response cycle.

In Session state variable -

request.getSession().setAttribute("sessionvariable", "My Value");

will save the value in multiple request - response cycle in a same browser session.

Also the value of the variable can be removed by

request.getSession().removeAttribute("sessionvariable");

This session variable value can be served in multiple jsp/servlets in application like user shopping cart information in a session.

So the Servlet works here as controller and the state of the variables are stored in session or request in the framework specified api and variables.

Piyas De
  • 1,786
  • 15
  • 27
0

A servlet also has access to the session created by your container(appsrver) through request.getSession()

Using the session a servlet can do a session management like storing cart items in a session and making it accessible on different requests from the client.

Avinash Singh
  • 3,421
  • 2
  • 20
  • 21