ServletContext类的简单使用

 1 package com.yxfyg.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class LoginServlet extends HttpServlet{
12     
13     @Override
14     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         String username = req.getParameter("username");
16         String password = req.getParameter("password");
17         
18         if("admin".equals(username)&&"root".equals(password)) {
19             //设置状态码:重新定位
20             resp.setStatus(302);
21             //跳转到哪一个页面
22             resp.setHeader("Location", "login_success.html");
23             int count = 0;
24             //获取Web工程的ServletContext对象
25             ServletContext sc = getServletContext();
26             if(sc.getAttribute("count") != null) {
27                 count = (int) sc.getAttribute("count");
28             }
29             sc.setAttribute("count", count+1);
30         }else {
31             resp.getWriter().println("login failed");
32         }
33     }
34     
35     @Override
36     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
37         doGet(req, resp);
38     }
39     
40 }
 1 package com.yxfyg.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class LoginServlet extends HttpServlet{
12     
13     @Override
14     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15         String username = req.getParameter("username");
16         String password = req.getParameter("password");
17         
18         if("admin".equals(username)&&"root".equals(password)) {
19             //设置状态码:重新定位
20             resp.setStatus(302);
21             //跳转到哪一个页面
22             resp.setHeader("Location", "login_success.html");
23             int count = 0;
24             //获取Web工程的ServletContext对象
25             ServletContext sc = getServletContext();
26             if(sc.getAttribute("count") != null) {
27                 count = (int) sc.getAttribute("count");
28             }
29             sc.setAttribute("count", count+1);
30         }else {
31             resp.getWriter().println("login failed");
32         }
33     }
34     
35     @Override
36     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
37         doGet(req, resp);
38     }
39     
40 }
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>login</title>
 6 </head>
 7 <body>
 8     <form action="LoginServlet" method="get">
 9         <table>
10             <tr>
11                 <td>用户名:</td>
12                 <td><input type="text" name="username" /></td>
13             </tr>
14             <tr>
15                 <td>密码:</td>
16                 <td><input type="text" name="password" /></td>
17             </tr>
18             <tr>
19                 <td></td>
20                 <td><input type="submit" value="登录" /></td>
21             </tr>
22         </table>
23     </form>
24 </body>
25 </html>
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>LoginSuccess</title>
 6 </head>
 7 <body>
 8     <h3>登录成功</h3>
 9     <a href="CountServlet">成功登录的次数</a>
10 </body>
11 </html>

 

posted @ 2020-04-29 13:31  yxfyg  阅读(171)  评论(0)    收藏  举报