Java Reference
In-Depth Information
The third session method returns an array of the current bound names stored in the session.
This method is convenient if you want to remove all the current bindings in a session. Its sig-
nature is listed as follows:
public String[] getAttributeNames()
The last session method is the removeAttribute() method. As its name suggests, it removes a
binding from the current session. It takes a string parameter representing the name associated
with the binding. Its method signature is listed as follows:
public void removeAttribute(String name )
Now that I have discussed the HttpSession object, let's take a look at an example of how to
use it. In this example, you will service a request that contains a list of movies to add to a
user's account. You will then parse the submitted list, add it to the customer's session, and
redisplay it for approval. When the customer approves the list, they will click the Proceed to
Checkout button to commit the transaction. Listing 5.4 contains the source for this example.
L ISTING 5.4
HttpSessionServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpSessionServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
//Process the HTTP Get request, this method
// will handle the checkout
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String[] movies = null;
// Get a handle to the HttpSession Object
// if there is no session create one
HttpSession session = request.getSession(true);
// Get the movies list object bound to the
Search WWH ::




Custom Search