wood burning stoves 2.0*
The moose likes Servlets and the fly likes Hypothetical Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Hypothetical Question" Watch "Hypothetical Question" New topic

Hypothetical Question

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
I just want to understand if I know how permanent Servlets work.

Scenario: Lets say I have a webpage with 2 txt input: Name: and what to do:

When the user fills in the two boxes and clicks submit, a page will load that says: what the servlet used to be doing:
<oldName> told me to do: <oldTask>
and Now: <newName> has told me to do : <newTask>

So i have my servlet, which takes the HTTP request and gets the two parameters: newName and newTask. The servlet has two instance variables: String oldName and String oldTask. (lets assume they have getters and setters).

So after the HTTPrequest has got
String newName = //whatever;
String newTask = //whatever;

Then, the most simple return would be:
write(HTMLHeaders etc);
write(getName() told me to do: getTask());
write(and Now: getName() told me to do: getTask());

I'm hoping i have the right end of the stick here. So anybody could go to the page and tell the servlet what to do and see what the previous task was. Does anything inparticular have to be done to make the Servlet permanent?


Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Tom,

What you're saying is basically correct. I wouldn't use the word permanent, however. What you're actually doing is maintaining the state of the session between requests. In order to do that you need to use code similar to the following:

You'd have to put some conditional stuff in there to handle the initial invocation and also include the rest of the HTML output, but that's about it. You also need to define the servlet and its mapping in web.xml.

This is a rather crappy way of implementing a dynamic page, btw; better to use a JSP for the display if you actually want to do it for real.

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
So the sate of the servlet is held open by the session - I assume its possible to set the lengtyh of the timeout for the session? Is it possible to say it lasts forever in the DD?
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
All the good stuff's there in the Javadoc.

If you setMaxInactiveInterval() to a negative number you get a session that never times out.

Don't confuse the session context (session) and the servlet context (application). There can be many sessions in a servlet/application.

wood burning stoves
subject: Hypothetical Question
Similar Threads
Java Application to Serlvet Problem.
Setting Bean property
pass by value
JNDI Aliasing
files are not beened deleted and renamed