Ta tạo thư mục dự án như sau :
Lớp AuthenticationServlet định nghĩa Servlet chứng thực như sau :
package com.openspace.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/logon") public class AuthenticationServlet extends HttpServlet { private static final long serialVersionUID = 5088499345088449403L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); // sendPage(req, resp, req.getSession(false)); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if ("toan".equals(req.getParameter("username")) && "servlet".equals(req.getParameter("password"))) { HttpSession session = req.getSession(true); sendPage(req, resp, session); } else { HttpSession session = req.getSession(false); if (session != null) session.invalidate(); sendPage(req, resp, null); } } private void sendPage(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws IOException { resp.setContentType("text/html"); PrintWriter pw = resp.getWriter(); pw.print("<html><head><title>Log on</title></head><body bgcolor=\"white\">"); if (session == null) { pw.print("<form name=\"logonForm\" method=\"post\" action=\"/Servlet/logon\">" + "<table border=\"0\" >" + "<tr>" + "<th class=\"right\">User name</th>" + "<td class=\"left\"><input type=\"text\" name=\"username\" size=\"30\" ></td>" + "</tr>" + "<tr>" + "<th class=\"right\">Password</th>" + "<td class=\"left\"><input type=\"password\" name=\"password\" size=\"30\">" + "</td>" + "</tr>" + "<tr>" + "<th class=\"right\"><input type=\"submit\" name=\"logon\" value=\"Log on\"></th>" + "<td class=\"right\"><input type=\"reset\" value=\"Reset\"></td>" + "</tr>" + "</table>" + "</form></body></html>"); } else { pw.print("Welcome " + req.getParameter("username") + "<br/>" + "<form name=\"logoffForm\" method=\"post\" action=\"/Servlet/logon\">" + "<input type=\"submit\" name=\"logoff\" value=\"Log out\">" + "</form></body></html>"); } } }
Giải thích :
- Lớp AuthenticationServlet là HTTP servlet vì nó mở rộng (extend) từ lớp HttpServlet.
- Dẫn giải (annotation) @WebServlet định nghĩa việc ánh xạ (mapping) đường dẫn "/logon" vào servlet AuthenticationServlet. Tức, khi người dùng thực hiện truy vấn đến địa chỉ "http://localhost:8080/Servlet/logon", bộ chứa servlet sẽ chuyển cho servlet AuthenticationServlet xử lý truy vấn này để gởi câu trả lời. Ở đây ta dùng dẫn giải để thực hiện việc ánh xạ, ngoài ra ta có thể thực hiện việc này qua tập tin cấu hình web.xml
- Lớp này hiện dựng (implement) hai phương thức doPost() và doGet() để hỗ trợ phương thức GET và POST. Tức, người dùng thực hiện truy vấn GET hay POST đến địa chỉ "http://localhost:8080/Servlet/logon" đều được servlet xử lý.
Triển khai :
Ở đây ta dùng Tomcat v7.0 với vai trò là máy chủ web và bộ chứa web (nhấp chuột phải trên dự án Servlet, chọn Run As> Run on Server, rồi chọn Tomcat v7.0) để triển khai ứng dụng (deploy application).
- Khi người dùng gửi truy vấn "GET http://localhost:8080/Servlet/logon?username=toan&password=servlet" hoặc truy vấn "POST http://localhost:8080/Servlet/logon" với tham số "username=toan&password=servlet", sẽ nhận được trả lời sau :
<html> <head> <title>Log on</title> </head> <body bgcolor="white"> Welcome toan <br /> <form name="logoffForm" method="post" action="/Servlet/logon"> <input type="submit" name="logoff" value="Log out"> </form> </body> </html>
- Khi người dùng gửi truy vấn "GET http://localhost:8080/Servlet/logon" hoặc "POST http://localhost:8080/Servlet/logon" với các tham số có giá trị khác với "username=toan&password=servlet", sẽ nhận được trả lời sau :
<html> <head> <title>Log on</title> </head> <body bgcolor="white"> <form name="logonForm" method="post" action="/Servlet/logon"> <table border="0"> <tr> <th class="right">User name</th> <td class="left"><input type="text" name="username" size="30"></td> </tr> <tr> <th class="right">Password</th> <td class="left"><input type="password" name="password" size="30"></td> </tr> <tr> <th class="right"><input type="submit" name="logon" value="Log on"></th> <td class="right"><input type="reset" value="Reset"></td> </tr> </table> </form> </body> </html>
Không có nhận xét nào:
Đăng nhận xét