Java Reference
In-Depth Information
LISTING 21.3
Continued
48: // user has submitted the login form
49: out.println(“Logging in “ + username);
50: session.setAttribute(“username”, username);
51: session.setAttribute(“password”, password);
52: session.setAttribute(“loggedIn”, new Boolean(true));
53: session.setAttribute(“lastVisit”, new Date());
54: out.println(“<a href=\”LoginServlet\”>Reload Page</a>”);
55: }
56: out.println(“</body>”);
57: out.println(“</html>”);
58: }
59: }
60:
61: public void doGet(HttpServletRequest req, HttpServletResponse res)
62: throws ServletException, IOException {
63:
64: doPost(req, res);
65: }
66: }
When the servlet is loaded for the first time in a web browser, it presents a form as
shown earlier in Figure 21.3.
Filling out the form and clicking the Submit button displays a page that has the text
“Logging in” and a Reload Page hyperlink.
Clicking the hyperlink loads a page with a greeting such as the following:
Welcome back, rcade
You last visited on Sat Feb 29 18:04:45 EST 2007
The servlet does not contain any code to check whether the provided username and pass-
word are valid. It simply stores them in a session so that they're available when the
servlet is run again subsequently.
JSP
Java servlets make it easy to generate HTML text dynamically, producing pages that
change in response to user input and data.
However, servlets make it difficult to generate HTML text that never changes because it
is cumbersome and tedious to use Java statements to output HTML.
 
 
Search WWH ::




Custom Search