1 login.jsp
2
3 <%@page contentType="text/html" pageEncoding="UTF-8"%>
4 <%
5 String path = request.getContextPath();
6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
7 %>
8
9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13
14 <title>用户登录界面</title>
15
16 <meta http-equiv="pragma" content="no-cache">
17 <meta http-equiv="cache-control" content="no-cache">
18 <meta http-equiv="expires" content="0">
19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20 <meta http-equiv="description" content="This is my page">
21 <!--
22 <link rel="stylesheet" type="text/css" href="styles.css">
23 -->
24
25 </head>
26
27 <body>
28 <form action="loginCheck" method="post">
29 用户名: <input type="text" name="name"><br><br>
30 密码: <input type="password" name="password"><br><br>
31 <input type="submit" value="确 认">
32 <input type="reset" value="取 消">
33 </form>
34 </body>
35 </html>
36
37
38 LoginCheckServlet.java
39
40 package servlets;
41
42 import java.io.IOException;
43 //import java.io.PrintWriter;
44
45 import javax.servlet.ServletException;
46 import javax.servlet.http.HttpServlet;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
50 public class LoginCheckServlet extends HttpServlet {
51
52
53 /**
54 *
55 */
56 private static final long serialVersionUID = 1L;
57
58 public void doPost(HttpServletRequest request, HttpServletResponse response)
59 throws ServletException, IOException {
60 String Name=request.getParameter("name");
61 String Password=request.getParameter("password");
62 String info="";
63 if(("abc".equals(Name))&&("123".equals(Password))){
64 info="欢迎你的登录"+Name+"!";}
65 else
66 {info="登陆失败!";}
67 request.setAttribute("outputMessage",info);
68 request.getRequestDispatcher("info.jsp").forward(request,response);
69 }
70 //response.setContentType("text/html");
71
72
73 }
74
75 info.jsp
76
77 <%@ page pageEncoding="UTF-8"%>
78 <%
79 String path = request.getContextPath();
80 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
81 %>
82
83 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
84 <html>
85 <head>
86 <base href="<%=basePath%>">
87
88 <title>显示页面</title>
89
90 <meta http-equiv="pragma" content="no-cache">
91 <meta http-equiv="cache-control" content="no-cache">
92 <meta http-equiv="expires" content="0">
93 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
94 <meta http-equiv="description" content="This is my page">
95 <!--
96 <link rel="stylesheet" type="text/css" href="styles.css">
97 -->
98
99 </head>
100
101 <body>
102 <%=request.getAttribute("outputMessage") %>
103 </body>
104 </html>