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.
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?
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.
Joined: Aug 24, 2003
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?