Professional Documents
Culture Documents
Workshop06 PRJ321 Tran PDF
Workshop06 PRJ321 Tran PDF
Workshop06 PRJ321 Tran PDF
Students kindly practice these questions in class and at home based on the
Lecturer’s guidance. Thank you :-D
Question 1: Using filter
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\src\java\model\
User.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package model;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public class User {
13 private String username;
14 private String password;
15
16 public User() {
17 }
18
19 public User(String username, String password) {
20 this.username = username;
21 this.password = password;
22 }
23
24 public String getUsername() {
25 return username;
26 }
27
28 public void setUsername(String username) {
29 this.username = username;
30 }
31
32 public String getPassword() {
33 return password;
34 }
35
36 public void setPassword(String password) {
37 this.password = password;
38 }
39
40
41 }
42
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\src\java\model\
UserService.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package model;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public class UserService {
13 public boolean checkLogin(User user) {
14 if (user.getUsername().equals("admin") &&
user.getPassword().equals("123456")) {
15 return true;
16 }
17 return false;
18 }
19 }
20
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\web\main.css
1 /*
2 To change this license header, choose License Headers in Project Properties.
3 To change this template file, choose Tools | Templates
4 and open the template in the editor.
5 */
6 /*
7 Created on : 18/10/2020, 8:35:54 PM
8 Author : Ly Quynh Tran
9 */
10 body{
11 font-family: Arial, sans-serif;
12 font-size: 11pt;
13 margin-left: 2em;
14 margin-right: 2em;
15 width: 400px;
16 border: 2px solid black;}
17 h1, h2, h3, h4, h5, h6{
18 color: teal;
19 text-align: center; }
20 label{
21 float: left;
22 width:10em;
23 text-align: right;
24 margin-bottom: .5em; }
25 input{
26 width: 15em;
27 margin-left: .5em;
28 margin-bottom: 1em;
29 margin-bottom: 1em;}
30 #id{
31 width: 10em;
32 text-align: center; }
33 br{clear: both;}
34
35
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\web\login.jsp
1 <%--
2 Document : login
3 Created on : 18/10/2020, 8:37:56 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
12 <title>Login Page</title>
13 <link href="main.css" rel="stylesheet" type="text/css"/>
14 </head>
15 <body>
16 <h1>Login to the FPT System</h1>
17 <%
18 String err = request.getParameter("err");
19
20 if ("1".equals(err)) {
21 out.print("<h4 style=\"color: red;\">Login Fail</h4>");
22 }
23 %>
24 <form name="myForm" action="homepage" method="POST">
25 <table border="0">
26
27 <tbody>
28 <tr>
29 <td>User Name: </td>
30 <td><input type="text" name="username" value="" size="50"
/></td>
31
32 </tr>
33 <tr>
34 <td>Password: </td>
35 <td><input type="password" name="password" value=""
size="50" /></td>
36
37 </tr>
38
39 </tbody>
40 </table>
41 <hr>
42
43 <input type="submit" value="Login" name="submit" />
44
45 </form>
46 </body>
47 </html>
48
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\src\java\controll
er\LoginController.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package controller;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.RequestDispatcher;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import model.User;
16 import model.UserService;
17
18 /**
19 *
20 * @author Ly Quynh Tran
21 */
22 public class LoginController extends HttpServlet {
23
24 /**
25 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
26 * methods.
27 *
28 * @param request servlet request
29 * @param response servlet response
30 * @throws ServletException if a servlet-specific error occurs
31 * @throws IOException if an I/O error occurs
32 */
33 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
34 throws ServletException, IOException {
35 response.setContentType("text/html;charset=UTF-8");
36 try (PrintWriter out = response.getWriter()) {
37 /* TODO output your page here. You may use following sample code.
*/
38 out.println("<!DOCTYPE html>");
39 out.println("<html>");
40 out.println("<head>");
41 out.println("<title>Servlet LoginController</title>");
42 out.println("</head>");
43 out.println("<body>");
44 out.println("<h1>Servlet LoginController at " +
request.getContextPath() + "</h1>");
45 out.println("</body>");
46 out.println("</html>");
47 }
48 }
49
50 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
51 /**
52 * Handles the HTTP <code>GET</code> method.
53 *
54 * @param request servlet request
55 * @param response servlet response
56 * @throws ServletException if a servlet-specific error occurs
57 * @throws IOException if an I/O error occurs
58 */
59 @Override
60 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
61 throws ServletException, IOException {
62 RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
63 rd.forward(request, response);
64 }
65
66 /**
67 * Handles the HTTP <code>POST</code> method.
68 *
69 * @param request servlet request
70 * @param response servlet response
71 * @throws ServletException if a servlet-specific error occurs
72 * @throws IOException if an I/O error occurs
73 */
74 @Override
75 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
76 throws ServletException, IOException {
77 String username = request.getParameter("username");
78 String password = request.getParameter("password");
79
80 User user = new User(username, password);
81 UserService service = new UserService();
82
83 if (service.checkLogin(user)) {
84 response.sendRedirect("success.jsp");
85 } else {
86 response.sendRedirect("login.jsp?err=1");
87 }
88 }
89
90 /**
91 * Returns a short description of the servlet.
92 *
93 * @return a String containing servlet description
94 */
95 @Override
96 public String getServletInfo() {
97 return "Short description";
98 }// </editor-fold>
99
100 }
101
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\web\success.jsp
1 <%--
2 Document : success
3 Created on : 18/10/2020, 8:38:43 PM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
12 <title>JSP Success</title>
13 </head>
14 <body>
15 <h1>You login successful</h1>
16 </body>
17 </html>
18
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\src\java\filter\fil
ter.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package filter;
7
8 import java.io.IOException;
9 import java.io.PrintStream;
10 import java.io.PrintWriter;
11 import java.io.StringWriter;
12 import javax.servlet.Filter;
13 import javax.servlet.FilterChain;
14 import javax.servlet.FilterConfig;
15 import javax.servlet.ServletException;
16 import javax.servlet.ServletRequest;
17 import javax.servlet.ServletResponse;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20
21 /**
22 *
23 * @author Ly Quynh Tran
24 */
25 public class filter implements Filter {
26
27 private static final boolean debug = true;
28
29 // The filter configuration object we are associated with. If
30 // this value is null, this filter instance is not currently
31 // configured.
32 private FilterConfig filterConfig = null;
33
34 public filter() {
35 }
36
37 private void doBeforeProcessing(ServletRequest request,
ServletResponse response)
38 throws IOException, ServletException {
39 if (debug) {
40 log("filter:DoBeforeProcessing");
41 }
42
43 // Write code here to process the request and/or response before
44 // the rest of the filter chain is invoked.
45 // For example, a logging filter might log items on the request object,
46 // such as the parameters.
47 /*
48 for (Enumeration en = request.getParameterNames();
en.hasMoreElements(); ) {
49 String name = (String)en.nextElement();
50 String values[] = request.getParameterValues(name);
51 int n = values.length;
52 StringBuffer buf = new StringBuffer();
53 buf.append(name);
54 buf.append("=");
55 for(int i=0; i < n; i++) {
56 buf.append(values[i]);
57 if (i < n-1)
58 buf.append(",");
59 }
60 log(buf.toString());
61 }
62 */
63 }
64
65 private void doAfterProcessing(ServletRequest request,
ServletResponse response)
66 throws IOException, ServletException {
67 if (debug) {
68 log("filter:DoAfterProcessing");
69 }
70
71 // Write code here to process the request and/or response after
72 // the rest of the filter chain is invoked.
73 // For example, a logging filter might log the attributes on the
74 // request object after the request has been processed.
75 /*
76 for (Enumeration en = request.getAttributeNames();
en.hasMoreElements(); ) {
77 String name = (String)en.nextElement();
78 Object value = request.getAttribute(name);
79 log("attribute: " + name + "=" + value.toString());
80
81 }
82 */
83 // For example, a filter might append something to the response.
84 /*
85 PrintWriter respOut = new PrintWriter(response.getWriter());
86 respOut.println("<P><B>This has been appended by an intrusive
filter.</B>");
87 */
88 }
89
90 /**
91 *
92 * @param request The servlet request we are processing
93 * @param response The servlet response we are creating
94 * @param chain The filter chain we are processing
95 *
96 * @exception IOException if an input/output error occurs
97 * @exception ServletException if a servlet error occurs
98 */
99 public void doFilter(ServletRequest request, ServletResponse response,
100 FilterChain chain)
101 throws IOException, ServletException {
102
103 if (debug) {
104 log("filter:doFilter()");
105 }
106
107 doBeforeProcessing(request, response);
108 HttpServletRequest httpRequest = (HttpServletRequest) request;
109 HttpServletResponse httpResponse = (HttpServletResponse)
response;
110 String url = httpRequest.getServletPath();
111 if (url.contains("login.jsp")) {
112 httpResponse.sendRedirect("homepage");
113 }
114
115 Throwable problem = null;
116 try {
117 chain.doFilter(request, response);
118 } catch (Throwable t) {
119 // If an exception is thrown somewhere down the filter chain,
120 // we still want to execute our after processing, and then
121 // rethrow the problem after that.
122 problem = t;
123 t.printStackTrace();
124 }
125
126 doAfterProcessing(request, response);
127
128 // If there was a problem, we want to rethrow it if it is
129 // a known type, otherwise log it.
130 if (problem != null) {
131 if (problem instanceof ServletException) {
132 throw (ServletException) problem;
133 }
134 if (problem instanceof IOException) {
135 throw (IOException) problem;
136 }
137 sendProcessingError(problem, response);
138 }
139 }
140
141 /**
142 * Return the filter configuration object for this filter.
143 */
144 public FilterConfig getFilterConfig() {
145 return (this.filterConfig);
146 }
147
148 /**
149 * Set the filter configuration object for this filter.
150 *
151 * @param filterConfig The filter configuration object
152 */
153 public void setFilterConfig(FilterConfig filterConfig) {
154 this.filterConfig = filterConfig;
155 }
156
157 /**
158 * Destroy method for this filter
159 */
160 public void destroy() {
161 }
162
163 /**
164 * Init method for this filter
165 */
166 public void init(FilterConfig filterConfig) {
167 this.filterConfig = filterConfig;
168 if (filterConfig != null) {
169 if (debug) {
170 log("filter:Initializing filter");
171 }
172 }
173 }
174
175 /**
176 * Return a String representation of this object.
177 */
178 @Override
179 public String toString() {
180 if (filterConfig == null) {
181 return ("filter()");
182 }
183 StringBuffer sb = new StringBuffer("filter(");
184 sb.append(filterConfig);
185 sb.append(")");
186 return (sb.toString());
187 }
188
189 private void sendProcessingError(Throwable t, ServletResponse
response) {
190 String stackTrace = getStackTrace(t);
191
192 if (stackTrace != null && !stackTrace.equals("")) {
193 try {
194 response.setContentType("text/html");
195 PrintStream ps = new PrintStream(response.getOutputStream());
196 PrintWriter pw = new PrintWriter(ps);
197
pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n");
//NOI18N
198
199 // PENDING! Localize this for next official release
200 pw.print("<h1>The resource did not process
correctly</h1>\n<pre>\n");
201 pw.print(stackTrace);
202 pw.print("</pre></body>\n</html>"); //NOI18N
203 pw.close();
204 ps.close();
205 response.getOutputStream().close();
206 } catch (Exception ex) {
207 }
208 } else {
209 try {
210 PrintStream ps = new PrintStream(response.getOutputStream());
211 t.printStackTrace(ps);
212 ps.close();
213 response.getOutputStream().close();
214 } catch (Exception ex) {
215 }
216 }
217 }
218
219 public static String getStackTrace(Throwable t) {
220 String stackTrace = null;
221 try {
222 StringWriter sw = new StringWriter();
223 PrintWriter pw = new PrintWriter(sw);
224 t.printStackTrace(pw);
225 pw.close();
226 sw.close();
227 stackTrace = sw.getBuffer().toString();
228 } catch (Exception ex) {
229 }
230 return stackTrace;
231 }
232
233 public void log(String msg) {
234 filterConfig.getServletContext().log(msg);
235 }
236
237 }
238
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebLoginFilterDemo\web\WEB-
INF\web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3
4 <filter>
5 <filter-name>filter</filter-name>
6 <filter-class>filter.filter</filter-class>
7 </filter>
8 <filter-mapping>
9 <filter-name>filter</filter-name>
10 <url-pattern>/*</url-pattern>
11 </filter-mapping>
12
13 <servlet>
14 <servlet-name>LoginController</servlet-name>
15 <servlet-class>controller.LoginController</servlet-class>
16 </servlet>
17 <servlet-mapping>
18 <servlet-name>LoginController</servlet-name>
19 <url-pattern>/homepage</url-pattern>
20 </servlet-mapping>
21 <session-config>
22 <session-timeout>
23 30
24 </session-timeout>
25 </session-config>
26 </web-app>
27
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\src\java\c
ontext\DatabaseInfor.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package context;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public interface DatabaseInfor {
13
14 public static String driverName =
"com.microsoft.sqlserver.jdbc.SQLServerDriver";
15 public static String url =
"jdbc:sqlserver://127.0.0.1:1433;databaseName=UserLogin;";
16 public static String user = "sa";
17 public static String pass = "abc123";
18 }
19
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\src\java\c
ontext\ConnectDB.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package context;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10
11 /**
12 *
13 * @author Ly Quynh Tran
14 */
15 public class ConnectDB implements DatabaseInfor{
16 private static ConnectDB instance;
17
18 public ConnectDB() {
19 }
20
21 public Connection OpenConnection() throws Exception {
22
23 Class.forName(driverName);
24 Connection con = DriverManager.getConnection(url, user, pass);
25 return con;
26 }
27 //Get instance of dbms only one time
28 public static ConnectDB getInstance(){
29 if(instance==null) instance = new ConnectDB();
30 return instance;
31 }
32 }
33
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\src\java\d
ao\TestDB.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package dao;
7
8 /**
9 *
10 * @author Ly Quynh Tran
11 */
12 public class TestDB {
13
14 /**
15 * @param args the command line arguments
16 */
17 public static void main(String[] args) {
18 // TODO code application logic here
19 UserDAO userDAO = new UserDAO();
20 String pw= userDAO.getPassword("Admin");
21 System.out.println(""+pw);
22 }
23
24 }
25
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\src\java\c
ontroller\LoginServlet.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package controller;
7
8 import dao.UserDAO;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 /**
17 *
18 * @author Ly Quynh Tran
19 */
20 public class LoginServlet extends HttpServlet {
21
22 /**
23 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
24 * methods.
25 *
26 * @param request servlet request
27 * @param response servlet response
28 * @throws ServletException if a servlet-specific error occurs
29 * @throws IOException if an I/O error occurs
30 */
31 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
32 throws ServletException, IOException {
33 response.setContentType("text/html;charset=UTF-8");
34 try (PrintWriter out = response.getWriter()) {
35 /* TODO output your page here. You may use following sample code.
*/
36 out.println("<!DOCTYPE html>");
37 out.println("<html>");
38 out.println("<head>");
39 out.println("<title>Servlet LoginServlet</title>");
40 out.println("</head>");
41 out.println("<body>");
42 out.println("<h1>Servlet LoginServlet at " +
request.getContextPath() + "</h1>");
43 out.println("</body>");
44 out.println("</html>");
45 }
46 }
47
48 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
49 /**
50 * Handles the HTTP <code>GET</code> method.
51 *
52 * @param request servlet request
53 * @param response servlet response
54 * @throws ServletException if a servlet-specific error occurs
55 * @throws IOException if an I/O error occurs
56 */
57 @Override
58 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
59 throws ServletException, IOException {
60 processRequest(request, response);
61 }
62
63 /**
64 * Handles the HTTP <code>POST</code> method.
65 *
66 * @param request servlet request
67 * @param response servlet response
68 * @throws ServletException if a servlet-specific error occurs
69 * @throws IOException if an I/O error occurs
70 */
71 @Override
72 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
73 throws ServletException, IOException {
74 String username = request.getParameter("username");
75 String password = request.getParameter("password");
76 UserDAO user = new UserDAO();
77 String checkPassword = user.getPassword(username).trim();
78 if (password.equals(checkPassword)) {
79 request.getSession().setAttribute("username", username);
80 response.sendRedirect(request.getContextPath() + "/Servlet1");
81 } else {
82 response.sendRedirect(request.getContextPath() + "/login.jsp");
83 }
84 }
85
86 /**
87 * Returns a short description of the servlet.
88 *
89 * @return a String containing servlet description
90 */
91 @Override
92 public String getServletInfo() {
93 return "Short description";
94 }// </editor-fold>
95
96 }
97
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\src\java\c
ontroller\Servlet1.java
1 /*
2 * To change this license header, choose License Headers in Project
Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package controller;
7
8 import java.io.IOException;
9 import java.io.PrintWriter;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 /**
16 *
17 * @author Ly Quynh Tran
18 */
19 public class Servlet1 extends HttpServlet {
20
21 /**
22 * Processes requests for both HTTP <code>GET</code> and
<code>POST</code>
23 * methods.
24 *
25 * @param request servlet request
26 * @param response servlet response
27 * @throws ServletException if a servlet-specific error occurs
28 * @throws IOException if an I/O error occurs
29 */
30 protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
31 throws ServletException, IOException {
32 response.setContentType("text/html;charset=UTF-8");
33 try (PrintWriter out = response.getWriter()) {
34 /* TODO output your page here. You may use following sample code.
*/
35 out.println("<!DOCTYPE html>");
36 out.println("<html>");
37 out.println("<head>");
38 out.println("<title>Servlet Servlet1</title>");
39 out.println("</head>");
40 out.println("<body>");
41 out.println("<h1>Login Successful </h1>");
42 out.println("</body>");
43 out.println("</html>");
44 }
45 }
46
47 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
48 /**
49 * Handles the HTTP <code>GET</code> method.
50 *
51 * @param request servlet request
52 * @param response servlet response
53 * @throws ServletException if a servlet-specific error occurs
54 * @throws IOException if an I/O error occurs
55 */
56 @Override
57 protected void doGet(HttpServletRequest request, HttpServletResponse
response)
58 throws ServletException, IOException {
59 processRequest(request, response);
60 }
61
62 /**
63 * Handles the HTTP <code>POST</code> method.
64 *
65 * @param request servlet request
66 * @param response servlet response
67 * @throws ServletException if a servlet-specific error occurs
68 * @throws IOException if an I/O error occurs
69 */
70 @Override
71 protected void doPost(HttpServletRequest request,
HttpServletResponse response)
72 throws ServletException, IOException {
73 processRequest(request, response);
74 }
75
76 /**
77 * Returns a short description of the servlet.
78 *
79 * @return a String containing servlet description
80 */
81 @Override
82 public String getServletInfo() {
83 return "Short description";
84 }// </editor-fold>
85
86 }
87
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\web\login.
jsp
1 <%--
2 Document : login
3 Created on : 19/10/2020, 9:52:09 AM
4 Author : Ly Quynh Tran
5 --%>
6
7 <%@page contentType="text/html" pageEncoding="UTF-8"%>
8 <!DOCTYPE html>
9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-
8">
12 <title>JSP Page</title>
13 </head>
14 <body>
15 <h1>Login</h1>
16 <form action="LoginServlet" method="POST">
17 <label>User:
</label> <input
type="text" name="username">
18 <br>
19 <label>PassWord: </label><input type="password"
name="password">
20 <hr>
21 <input type="submit" value="login">
22 </form>
23 </body>
24 </html>
25
C:\Users\Ly Quynh
Tran\Documents\NetBeansProjects\WebBeforeLoginFilterDemo\web\WEB
-INF\web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
3 <filter>
4 <filter-name>AuthenticationFilter</filter-name>
5 <filter-class>filter.AuthenticationFilter</filter-class>
6 </filter>
7 <filter-mapping>
8 <filter-name>AuthenticationFilter</filter-name>
9 <url-pattern>/*</url-pattern>
10 </filter-mapping>
11 <servlet>
12 <servlet-name>LoginServlet</servlet-name>
13 <servlet-class>controller.LoginServlet</servlet-class>
14 </servlet>
15 <servlet>
16 <servlet-name>Servlet1</servlet-name>
17 <servlet-class>controller.Servlet1</servlet-class>
18 </servlet>
19 <servlet>
20 <servlet-name>Servlet2</servlet-name>
21 <servlet-class>controller.Servlet2</servlet-class>
22 </servlet>
23 <servlet-mapping>
24 <servlet-name>LoginServlet</servlet-name>
25 <url-pattern>/LoginServlet</url-pattern>
26 </servlet-mapping>
27 <servlet-mapping>
28 <servlet-name>Servlet1</servlet-name>
29 <url-pattern>/Servlet1</url-pattern>
30 </servlet-mapping>
31 <servlet-mapping>
32 <servlet-name>Servlet2</servlet-name>
33 <url-pattern>/Servlet2</url-pattern>
34 </servlet-mapping>
35 <session-config>
36 <session-timeout>
37 30
38 </session-timeout>
39 </session-config>
40 </web-app>
41