/Users/lyon/j4p/src/net/server/servlets/Login.java
|
1 package net.server.servlets;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.http.*;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7
8 /**
9 * Login Class
10 */
11
12 public class Login extends HttpServlet {
13
14 /**
15 * First call to this sevlet retuns the Login Page. Subsequent calls
16 * validate the userId and password. If entries are valid then the
17 * process is redirected to FormC Servlet
18 * @throws IOException ServletException
19 */
20
21 public void doGet(HttpServletRequest request,
22 HttpServletResponse response)
23 throws IOException, ServletException {
24
25 response.setContentType("text/html");
26 PrintWriter out = response.getWriter();
27
28 HttpSession session = request.getSession(true);
29 LoginProcess login = (LoginProcess) session.getValue("login");
30
31 if (session.isNew() || login == null) {
32 login = new LoginProcess();
33
34 Cookie loginCookie = new Cookie("Login", "LoginPage");
35 response.addCookie(loginCookie);
36
37 session.putValue("login", login);
38 out.println(login.getLoginPage());
39 } else {
40 String user = request.getParameter("txtUserId");
41 String password = request.getParameter("txtPassword");
42
43 System.out.println("\n Cookie Example");
44 System.out.println("\n Cookie Login = " +
45 RequestUtil.getCookieValue(request.getCookies(), "Login"));
46
47 if (login.isValid(user, password)) {
48 session.putValue("userId", user);
49 session.putValue("nextAction", "FormC");
50 response.sendRedirect(login.getLoginRedirectURL());
51 } else {
52 out.println(login.getLoginErrorPage());
53 }
54 }
55 }
56
57
58 /**
59 * Calls doGet() method
60 *
61 * @throws IOException ServletException
62 */
63
64 public void doPost(HttpServletRequest request,
65 HttpServletResponse response)
66 throws IOException, ServletException {
67
68 doGet(request, response);
69
70 }
71
72 }
73
74
75
76