This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Hypothetical Question" Watch "Hypothetical Question" New topic
Author

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());
setName(newName);
setTask(newTask);
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?

Cheers!

Tom
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.

Jules
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.

Jules
 
wood burning stoves
 
subject: Hypothetical Question