Servlets sessions

X

Xarky

Hi,
I have the following three servlets

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class Front extends HttpServlet
{
public void init(ServletConfig c)
{
(c.getServletContext()).log("Session Information Part - Front");
} // end method init


public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
PrintWriter out;
HttpSession session;

session = request.getSession(true);
response.setContentType("text/html");
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>" + "Enter Personal Data" +"</title>");
out.println ("</head>");
out.println ("<body bgcolor=\"white\">");
out.println ("<p>Enter data here:</p>");

out.println ("<form action=\"NameAndSurnameServlet\"
method=\"post\">");
out.println ("<p>Name : <input type=\"text\" size=20
name=\"personName\"> </p><br>");
out.println ("<p>Surname : <input type=\"text\" size=20
name=\"personSurname\"></p><br>");
out.println ("<input type=submit>");
out.println ("</form>");

out.println ("</body>");
out.println ("</html>");
} // end method doGet


} // end class Front


/***********************************************/
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class NameAndSurnameServlet extends HttpServlet
{
public void init(ServletConfig c)
{
(c.getServletContext()).log("Session Information Part - Name and
Surname servlet");
} // end method init

public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
PrintWriter out;
String name, surname;
HttpSession session;

session = request.getSession();
name = request.getParameter("personName");
surname = request.getParameter("personSurname");
//session.setAttribute("presonName", name);
//session.setAttribute("presonSurname", surname);
session.putValue("personName", name);
session.putValue("personSurname", surname);

response.setContentType("text/html");
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>" + "Processing Personal Data" +"</title>");
out.println ("</head>");
out.println ("<body bgcolor=\"white\">");
out.println ("<p>Processing</p>");

out.println ("<form action=\"DisplayAllServlet\"
method=\"post\">");
out.println ("<p>Age : <input type=\"text\" size=20
name=\"personAge\"> </p><br>");
out.println ("<input type=submit>");
out.println ("</form>");

out.println ("</body>");
out.println ("</html>");
} // end method doPost

} // end class NameAndSurnameServlet

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class DisplayAllServlet extends HttpServlet
{
public void init(ServletConfig c)
{
(c.getServletContext()).log("Session Information Part - Display All
Servlet");
} // end method init

public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
PrintWriter out;
String name, surname, age;
HttpSession session;

session = request.getSession();
response.setContentType("text/html");
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>" + "Showing Personal Data" +"</title>");
out.println ("</head>");
out.println ("<body bgcolor=\"white\">");
out.println ("<p></p>");

name = (String)session.getAttribute("personName");
surname = (String)session.getAttribute("personSurname");
age = request.getParameter("personAge");

out.println ("<p>Showing Personal Data</p>");
out.println ("<p>Name = "+name+"</p>");
out.println ("<p>Surname = "+surname+"</p>");
out.println ("<p>Age = "+age+"</p>");

out.println ("</body>");
out.println ("</html>");
} // end method doPost

} // end class DisplayAllServlet

Basically what I have is, in Front form, I am entering name and
surname, then on submit, NameAndSurnameServlet requests for age and
then the DisplayAllServlet displays the name surname and age of
person.

Now I was using sessions. I started the session in the Front Servlet.
In the NameAndSurnameServlet, retrieved the data(getParameters()) and
set them by using putValue(..). In the third servlet, I retrieved all
the data using getParameter(..) and getAttribute(..). As it is works.

Is in this way correct, because I was trying to set the data retrieved
from the second servlet by using setAttribute(..);(without
putValue(..) but then the name and surname being returned in 3rd
servlet were being null.

Now when I added the putValue(..), the following warning was being
given
"Note: C:\Practicals\NameAndSurnameServlet.java uses or overrides a
deprecated API."


Can someone give me some help
 
C

Chris Smith

Xarky said:
Is in this way correct, because I was trying to set the data retrieved
from the second servlet by using setAttribute(..);(without
putValue(..) but then the name and surname being returned in 3rd
servlet were being null.

putValue and setAttribute do exactly the same thing... but as you
discovered, putValue is deprecated and should not be used. As for why
setAttribute wasn't working:
//session.setAttribute("presonName", name);
//session.setAttribute("presonSurname", surname);
session.putValue("personName", name);
session.putValue("personSurname", surname);

You misspelled "person" in the attribute name of your setAttribute
calls.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top