博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA遇见HTML——Servlet篇:Servlet基础
阅读量:5025 次
发布时间:2019-06-12

本文共 36493 字,大约阅读时间需要 121 分钟。

 

 

 

 

 

 

 

 

 

代码实现:

HelloServlet
1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 //1.继承于HttpServlet11 public class HelloServlet extends HttpServlet {12 13     @Override14     protected void doGet(HttpServletRequest request, HttpServletResponse response)15             throws ServletException, IOException {16         // TODO Auto-generated method stub17         System.out.println("处理Get()请求...");18         //获得给浏览器输出的对象19         PrintWriter out =response.getWriter();20         //指定输出的文件类型,指定字符集21         response.setContentType("text/html;charset=utf-8");22         out.println("Hello Servlet!
");//html代码23 }24 25 @Override26 protected void doPost(HttpServletRequest request, HttpServletResponse response)27 throws ServletException, IOException {28 // TODO Auto-generated method stub29 System.out.println("处理Post()请求...");30 //获得给浏览器输出的对象31 PrintWriter out =response.getWriter();32 //指定输出的文件类型,指定字符集33 response.setContentType("text/html;charset=utf-8");34 out.println("Hello Servlet!
");//html代码35 }36 }

 

web.xml

1 
2
3
4
5
index.jsp
6
7 8
9
10
HelloServlet
11
12
servlet.HelloServlet
13
14 15
16
HelloServlet
17
18
19
/servlet/HelloServlet
20
21

index.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     12     My JSP 'index.jsp' starting page13     
14
15
16
17
18
21 22 23 24

第一个Servlet小例子

25

26
27 Get方式请求HelloServlet
28
29
30
31
32 33

 

 

 

1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 HelloServlet extends HttpServlet {12 13     /**14      * Constructor of the object.15      */16     public HelloServlet() {17         super();18     }19 20     /**21      * Destruction of the servlet. 
22 */23 public void destroy() {24 super.destroy(); // Just puts "destroy" string in log25 // Put your code here26 }27 28 /**29 * The doGet method of the servlet.
30 *31 * This method is called when a form has its tag value method equals to get.32 * 33 * @param request the request send by the client to the server34 * @param response the response send by the server to the client35 * @throws ServletException if an error occurred36 * @throws IOException if an error occurred37 */38 public void doGet(HttpServletRequest request, HttpServletResponse response)39 throws ServletException, IOException {40 System.out.println("处理Get请求...");41 42 response.setContentType("text/html;charset=utf-8");43 PrintWriter out = response.getWriter();44 out.println("");45 out.println("");46 out.println(" A Servlet");47 out.println(" ");48 out.print(" This is ");49 out.print(this.getClass());50 out.println(", using the GET method");51 out.println("Hello Servlet!
");52 out.println(" ");53 out.println("");54 out.flush();55 out.close();56 }57 58 /**59 * The doPost method of the servlet.
60 *61 * This method is called when a form has its tag value method equals to post.62 * 63 * @param request the request send by the client to the server64 * @param response the response send by the server to the client65 * @throws ServletException if an error occurred66 * @throws IOException if an error occurred67 */68 public void doPost(HttpServletRequest request, HttpServletResponse response)69 throws ServletException, IOException {70 System.out.println("处理Post请求...");71 72 response.setContentType("text/html;charset=utf-8");73 PrintWriter out = response.getWriter();74 out.println("");75 out.println("");76 out.println(" A Servlet");77 out.println(" ");78 out.print(" This is ");79 out.print(this.getClass());80 out.println(", using the POST method");81 out.println("Hello Servlet!
");82 out.println(" ");83 out.println("");84 out.flush();85 out.close();86 }87 88 /**89 * Initialization of the servlet.
90 *91 * @throws ServletException if an error occurs92 */93 public void init() throws ServletException {94 // Put your code here95 }96 97 }

web.xml

1 
2
6
7
This is the description of my J2EE component
8
This is the display name of my J2EE component
9
HelloServlet
10
servlet.HelloServlet
11
12 13
14
HelloServlet
15
/servlet/HelloServlet
16
17 18
19
index.jsp
20
21 22

index.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     12     My JSP 'index.jsp' starting page13     
14
15
16
17
18
21 22 23 24

使用MyEclipse创建Servlet小例子

25

26 Get方式请求HelloServlet
27
28
29
30 31

 

 

 

 

 

 

 

 

 

 

 

 

 

1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 TestServlet1 extends HttpServlet {12 13     /**14      * Constructor of the object.15      */16     public TestServlet1() {17         System.out.println("TestServlet1构造方法被执行...");18     }19 20     /**21      * Destruction of the servlet. 
22 */23 public void destroy() {24 System.out.println("TestServlet1销毁方法被执行...");25 }26 27 /**28 * The doGet method of the servlet.
29 *30 * This method is called when a form has its tag value method equals to get.31 * 32 * @param request the request send by the client to the server33 * @param response the response send by the server to the client34 * @throws ServletException if an error occurred35 * @throws IOException if an error occurred36 */37 public void doGet(HttpServletRequest request, HttpServletResponse response)38 throws ServletException, IOException {39 40 System.out.println("TestServlet1的doGet方法被执行...");41 42 response.setContentType("text/html;charset=utf-8");43 PrintWriter out = response.getWriter();44 out.println("");45 out.println("");46 out.println(" A Servlet");47 out.println(" ");48 out.print(" This is ");49 out.print(this.getClass());50 out.println(", using the GET method");51 out.println("

大家好,我是TestServlet1!

");52 out.println(" ");53 out.println("");54 out.flush();55 out.close();56 }57 58 /**59 * The doPost method of the servlet.
60 *61 * This method is called when a form has its tag value method equals to post.62 * 63 * @param request the request send by the client to the server64 * @param response the response send by the server to the client65 * @throws ServletException if an error occurred66 * @throws IOException if an error occurred67 */68 public void doPost(HttpServletRequest request, HttpServletResponse response)69 throws ServletException, IOException {70 71 System.out.println("TestServlet1的doPost方法被执行...");72 doGet(request,response);//让doPost执行与doGet()相同的操作73 }74 75 /**76 * Initialization of the servlet.
77 *78 * @throws ServletException if an error occurs79 */80 public void init() throws ServletException {81 System.out.println("TestServlet1初始化方法被执行...");82 }83 84 }

 

package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet2 extends HttpServlet {    /**     * Constructor of the object.     */    public TestServlet2() {        System.out.println("TestServlet2构造方法被执行...");    }    /**     * Destruction of the servlet. 
*/ public void destroy() { System.out.println("TestServlet2销毁方法被执行..."); } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TestServlet2的doGet方法被执行..."); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println("

大家好,我是TestServlet2!

"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TestServlet2的doPost方法被执行..."); doGet(request,response);//让doPost执行与doGet()相同的操作 } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("TestServlet2初始化方法被执行..."); }}

 

This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet1
servlet.TestServlet1
2
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet2
servlet.TestServlet2
1
TestServlet1
/servlet/TestServlet1
TestServlet2
/servlet/TestServlet2
index.jsp

 

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     12     My JSP 'index.jsp' starting page13     
14
15
16
17
18
21 22 23 24

Servlet生命周期

25 以Get方式请求TestServlet1
26 以Get方式请求TestServlet227 28

 

 

 

 

 

1 package entity; 2  3 import java.util.Date; 4  5 public class Users { 6  7     private String username;//用户名 8     private String mypassword;//密码 9     private String email;//电子邮箱10     private String gender;//性别11     private Date birthday;//出生日期12     private String[] favorites;//爱好13     private String introduce;//自我介绍14     private boolean flag;//是否接受协议15     16     public Users()17     {18         19     }20 21     public String getUsername() {22         return username;23     }24 25     public void setUsername(String username) {26         this.username = username;27     }28 29     public String getMypassword() {30         return mypassword;31     }32 33     public void setMypassword(String mypassword) {34         this.mypassword = mypassword;35     }36 37     public String getEmail() {38         return email;39     }40 41     public void setEmail(String email) {42         this.email = email;43     }44 45     public String getGender() {46         return gender;47     }48 49     public void setGender(String gender) {50         this.gender = gender;51     }52 53     public Date getBirthday() {54         return birthday;55     }56 57     public void setBirthday(Date birthday) {58         this.birthday = birthday;59     }60 61     public String[] getFavorites() {62         return favorites;63     }64 65     public void setFavorites(String[] favorites) {66         this.favorites = favorites;67     }68 69     public String getIntroduce() {70         return introduce;71     }72 73     public void setIntroduce(String introduce) {74         this.introduce = introduce;75     }76 77     public boolean isFlag() {78         return flag;79     }80 81     public void setFlag(boolean flag) {82         this.flag = flag;83     }84     85 }

 

1 package servlet;  2   3 import java.io.IOException;  4 import java.io.PrintWriter;  5 import java.text.ParseException;  6 import java.text.SimpleDateFormat;  7 import java.util.Date;  8   9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13  14 import entity.Users; 15  16 public class RegServlet extends HttpServlet { 17  18     /** 19      * Constructor of the object. 20      */ 21     public RegServlet() { 22         super(); 23     } 24  25     /** 26      * Destruction of the servlet. 
27 */ 28 public void destroy() { 29 super.destroy(); // Just puts "destroy" string in log 30 // Put your code here 31 } 32 33 /** 34 * The doGet method of the servlet.
35 * 36 * This method is called when a form has its tag value method equals to get. 37 * 38 * @param request the request send by the client to the server 39 * @param response the response send by the server to the client 40 * @throws ServletException if an error occurred 41 * @throws IOException if an error occurred 42 */ 43 public void doGet(HttpServletRequest request, HttpServletResponse response) 44 throws ServletException, IOException { 45 46 doPost(request,response); 47 } 48 49 /** 50 * The doPost method of the servlet.
51 * 52 * This method is called when a form has its tag value method equals to post. 53 * 54 * @param request the request send by the client to the server 55 * @param response the response send by the server to the client 56 * @throws ServletException if an error occurred 57 * @throws IOException if an error occurred 58 */ 59 public void doPost(HttpServletRequest request, HttpServletResponse response) 60 throws ServletException, IOException { 61 62 request.setCharacterEncoding("utf-8"); 63 64 Users u = new Users(); 65 String username,mypassword,gender,email,introduce,isAccept; 66 Date birthday; 67 String[] favorites; 68 69 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 70 try 71 { 72 username = request.getParameter("username");//返回指定参数的参数值 73 mypassword = request.getParameter("mypassword"); 74 gender = request.getParameter("gender"); 75 email = request.getParameter("email"); 76 introduce = request.getParameter("introduce"); 77 birthday = sdf.parse(request.getParameter("birthday"));//sdf.parse() 解析字符串的文本,生成 Date。 78 if(request.getParameterValues("isAccept")!=null) 79 { 80 isAccept=request.getParameter("isAccept"); 81 } 82 else 83 { 84 isAccept="false"; 85 } 86 //用来获取多个复选按钮的值 87 favorites = request.getParameterValues("favorite"); 88 89 u.setUsername(username); 90 u.setMypassword(mypassword); 91 u.setGender(gender); 92 u.setEmail(email); 93 u.setFavorites(favorites); 94 u.setIntroduce(introduce); 95 96 if(isAccept.equals("true")) 97 { 98 u.setFlag(true); 99 }100 else101 {102 u.setFlag(false);103 }104 u.setBirthday(birthday);105 //把注册成功的用户对象保存在session中106 request.getSession().setAttribute("regUser", u);//存储此请求中的属性107 //跳转到注册成功页面108 request.getRequestDispatcher("../userinfo.jsp").forward(request, response);//跳转到外层目录userinfo.jsp109 } catch (Exception ex) {110 // TODO Auto-generated catch block111 ex.printStackTrace();112 }113 }114 115 /**116 * Initialization of the servlet.
117 *118 * @throws ServletException if an error occurs119 */120 public void init() throws ServletException {121 // Put your code here122 }123 124 }

web.xml

1 
2
7
8
9
This is the description of my J2EE component
10
This is the display name of my J2EE component
11
RegServlet
12
servlet.RegServlet
13
14 15
16
RegServlet
17
/servlet/RegServlet
18
19
20
index.jsp
21
22

注册页面reg.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>  2 <%  3 String path = request.getContextPath();  4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  5 %>  6   7   8   9    10      11      12     My JSP 'reg.jsp' starting page 13      14     
15
16
17
18
19
22 30 31 32 33 34

用户注册

35

36
37
38
39
40
41
42
43
44
45 46
47
48
49
50 51
52
53
54
55 56
57
58
59
60 61
62 63
64
65
69
70
71
72
78
79
80
81
84
85
86
87
90
91
92
96
97
用户名:
密码:
确认密码:
电子邮箱:
性别:
出生日期: 66 68
爱好: 73 NBA   74 音乐   75 电影   76 上网   77
自我介绍: 82 83
接受协议: 88 是否接受霸王条款 89
93    94    95
98
99 100

注册成功页面userinfo.jsp

1 <%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%> 2  3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7  8  9 10   11     12     13     My JSP 'userinfo.jsp' starting page14     15     
16
17
18
19
20
23 35 36 37 38

用户信息

39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
69
70
71
72
83
84
85
86
87
88
89
90
91
92
用户名:  
密码:  
性别:  
E-mail:  
出生日期:  62 <% 63 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");64 String date = sdf.format(regUser.getBirthday());65 66 %>67 <%=date%>68
爱好:  73 <% 74 String[] favorites = regUser.getFavorites();75 for(String f:favorites)76 {77 %>78 <%=f%>   79 <% 80 }81 %>82
自我介绍:  
是否介绍协议:  
93
94 95

 

 

 

 

 

 

 

1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 HelloServlet extends HttpServlet {12 13     /**14      * Constructor of the object.15      */16     public HelloServlet() {17         super();18     }19 20     /**21      * Destruction of the servlet. 
22 */23 public void destroy() {24 super.destroy(); // Just puts "destroy" string in log25 // Put your code here26 }27 28 /**29 * The doGet method of the servlet.
30 *31 * This method is called when a form has its tag value method equals to get.32 * 33 * @param request the request send by the client to the server34 * @param response the response send by the server to the client35 * @throws ServletException if an error occurred36 * @throws IOException if an error occurred37 */38 public void doGet(HttpServletRequest request, HttpServletResponse response)39 throws ServletException, IOException {40 41 doPost(request,response);42 }43 44 /**45 * The doPost method of the servlet.
46 *47 * This method is called when a form has its tag value method equals to post.48 * 49 * @param request the request send by the client to the server50 * @param response the response send by the server to the client51 * @throws ServletException if an error occurred52 * @throws IOException if an error occurred53 */54 public void doPost(HttpServletRequest request, HttpServletResponse response)55 throws ServletException, IOException {56 57 response.setContentType("text/html;charset=utf-8");58 PrintWriter out = response.getWriter();59 out.println("");60 out.println("");61 out.println(" A Servlet");62 out.println(" ");63 out.print(" This is ");64 out.print(this.getClass());65 out.println(", using the POST method");66 out.println("

您好,我是HelloServlet!

");67 out.println(" ");68 out.println("");69 out.flush();70 out.close();71 }72 73 /**74 * Initialization of the servlet.
75 *76 * @throws ServletException if an error occurs77 */78 public void init() throws ServletException {79 // Put your code here80 }81 82 }

 

1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 TestServlet extends HttpServlet {12 13     /**14      * Constructor of the object.15      */16     public TestServlet() {17         super();18     }19 20     /**21      * Destruction of the servlet. 
22 */23 public void destroy() {24 super.destroy(); // Just puts "destroy" string in log25 // Put your code here26 }27 28 /**29 * The doGet method of the servlet.
30 *31 * This method is called when a form has its tag value method equals to get.32 * 33 * @param request the request send by the client to the server34 * @param response the response send by the server to the client35 * @throws ServletException if an error occurred36 * @throws IOException if an error occurred37 */38 public void doGet(HttpServletRequest request, HttpServletResponse response)39 throws ServletException, IOException {40 41 doPost(request,response);42 }43 44 /**45 * The doPost method of the servlet.
46 *47 * This method is called when a form has its tag value method equals to post.48 * 49 * @param request the request send by the client to the server50 * @param response the response send by the server to the client51 * @throws ServletException if an error occurred52 * @throws IOException if an error occurred53 */54 public void doPost(HttpServletRequest request, HttpServletResponse response)55 throws ServletException, IOException {56 57 //请求重定向方式跳转到test.jsp,当前路径是ServletPathDirection/servlet/58 response.sendRedirect("test.jsp");//404错误59 // response.sendRedirect("/test.jsp");//404错误60 //使用request.getContext获得上下文对象61 // response.sendRedirect(request.getContextPath()+"/test.jsp");62 63 64 65 //服务器内部跳转,这里的斜线表示项目的根目录66 // request.getRequestDispatcher("/test.jsp").forward(request, response);67 // request.getRequestDispatcher("../test.jsp").forward(request, response);68 69 }70 71 /**72 * Initialization of the servlet.
73 *74 * @throws ServletException if an error occurs75 */76 public void init() throws ServletException {77 // Put your code here78 }79 80 }

 

This is the description of my J2EE component
This is the display name of my J2EE component
HelloServlet
servlet.HelloServlet
This is the description of my J2EE component
This is the display name of my J2EE component
TestServlet
servlet.TestServlet
HelloServlet
/servlet/HelloServlet
TestServlet
/servlet/TestServlet
index.jsp

index.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     12     My JSP 'index.jsp' starting page13     
14
15
16
17
18
21 22 23 24

Servlet路径跳转

25

26
27
28 使用相对路径访问HelloServlet!
29
30 使用绝对路径访问HelloServlet!
31
32 访问TestServlet,跳转到Test.jsp33 34

test.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7  8  9   10     11     12     My JSP 'index.jsp' starting page13     
14
15
16
17
18
21 22 23 24

Test.jsp

25 26

 

 

1 package com.po; 2  3 public class Users { 4  5     private String username; 6     private String password; 7     public Users() { 8          9     }10     public String getUsername() {11         return username;12     }13     public void setUsername(String username) {14         this.username = username;15     }16     public String getPassword() {17         return password;18     }19     public void setPassword(String password) {20         this.password = password;21     }    22 }

 

1 
2
7
8
9
This is the description of my J2EE component
10
This is the display name of my J2EE component
11
LoginServlet
12
servlet.LoginServlet
13
14 15
16
LoginServlet
17
/servlet/LoginServlet
18
19
20
login.jsp
21
22

 

1 package servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5  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 import com.po.Users;12 13 public class LoginServlet extends HttpServlet {14 15     /**16      * Constructor of the object.17      */18     public LoginServlet() {19         super();20     }21 22     /**23      * Destruction of the servlet. 
24 */25 public void destroy() {26 super.destroy(); // Just puts "destroy" string in log27 // Put your code here28 }29 30 /**31 * The doGet method of the servlet.
32 *33 * This method is called when a form has its tag value method equals to get.34 * 35 * @param request the request send by the client to the server36 * @param response the response send by the server to the client37 * @throws ServletException if an error occurred38 * @throws IOException if an error occurred39 */40 public void doGet(HttpServletRequest request, HttpServletResponse response)41 throws ServletException, IOException {42 43 doPost(request,response);44 }45 46 /**47 * The doPost method of the servlet.
48 *49 * This method is called when a form has its tag value method equals to post.50 * 51 * @param request the request send by the client to the server52 * @param response the response send by the server to the client53 * @throws ServletException if an error occurred54 * @throws IOException if an error occurred55 */56 public void doPost(HttpServletRequest request, HttpServletResponse response)57 throws ServletException, IOException {58 59 Users u = new Users();60 String username=request.getParameter("username");61 String password=request.getParameter("password");62 u.setUsername(username);63 u.setPassword(password);64 //判断用户名和密码是否合法65 if(u.getUsername().equals("admin")&&u.getPassword().equals("admin"))66 {67 response.sendRedirect(request.getContextPath()+"/login_success.jsp");68 }69 else70 {71 response.sendRedirect(request.getContextPath()+"/login_failure.jsp");72 }73 74 request.getSession().setAttribute("loginUserid", u);//把注册成功的用户对象保存在session对象中75 }76 77 /**78 * Initialization of the servlet.
79 *80 * @throws ServletException if an error occurs81 */82 public void init() throws ServletException {83 // Put your code here84 }85 86 }

login.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3    String path = request.getContextPath(); 4    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7      8         
9 imooc - Login10
11
12
13
14 15 16 17
18 19 20
21
24
25
26

27 28 29 30 31

32

33 34

35
36
37
38 39

 

login_failure

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3    String path = request.getContextPath(); 4    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7      8         
9 imooc - Login10
11
12
13
14 15 16 17
18 19 20
21
24
25 登录失败!请检查用户或者密码!
26
返回登录 27
28
29 30

 

 login_success.jsp

1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3    String path = request.getContextPath(); 4    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7      8         
9 imooc - Login10
11
12
13
14 15 16 17
18 19 20
37 38
39
40
44
45 欢迎您
,登录成功!47
48
49 50 51 52

 

转载于:https://www.cnblogs.com/songsongblue/p/9755469.html

你可能感兴趣的文章
BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)
查看>>
java-动态规划算法学习笔记
查看>>
STL容器之vector
查看>>
Linux 内核中断内幕
查看>>
DNS负载均衡
查看>>
无法向会话状态服务器发出会话状态请求
查看>>
数据中心虚拟化技术
查看>>
Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
查看>>
01入门
查看>>
python正则表达式
查看>>
嵌套循环连接(nested loops join)原理
查看>>
shell统计特征数量
查看>>
复习文件操作
查看>>
git使用 ——转
查看>>
C#Hashtable与Dictionary性能
查看>>
10个让你忘记 Flash 的 HTML5 应用演示
查看>>
8个Python面试必考的题目,小编也被坑过 ToT
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
centos 图形界面和命令行界面切换(转载)
查看>>
Maven启用代理访问
查看>>