Java Reference
In-Depth Information
L ISTING 5.4
Continued
// name “Movies”
if ( session != null ) {
movies = (String[])session.getAttribute(“Movies”);
}
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<html>”);
out.println(“<head><title>Session Servlet</title></head>”);
out.println(“<body>”);
// Iterate over the movies array, displaying the
// current list of movies stored in the session
out.println(“<H2>Thank you for purchasing:</H2>”);
for ( int x = 0; x < movies.length; x++ ) {
out.println(movies[x] + “<BR>”);
}
out.println(“</body></html>”);
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Parse the movies selected
String movies[] = request.getParameterValues(“Movies”);
// Get a handle to the HttpSession Object
// if there is no session create one
HttpSession session = request.getSession(true);
// add the list of movies to the session
// binding it to the String “Movies”
if ( session != null ) {
5
session.setAttribute(“Movies”, movies);
}
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
 
Search WWH ::




Custom Search