Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Hypothetical Question

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic