日期:2014-05-16  浏览次数:20432 次

JSP-ServLet 一个简单的密码核对
First Step:

Create One Jsp file: Login.jsp

<body>
    This is my JSP page. <br>
    <form action="LoginServlet" method="post">
    <table>
    <tr><td>Login</td><td><input type="text" name="username" value=""/></td></tr>
    <tr><td>Password</td><td><input type="password" name="pwd" value=""/></td></tr>
    <tr><td><input type="submit" name="sub" value="Submit"/></td><td><input type="Reset" name="Ret" value="Reset"/></td></tr>
    </table>
    </form>
</body>

Second Step:

Create One ServLet File: LoginServLet.java
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name =request.getParameter("username");
String password=request.getParameter("pwd");
HttpSession session=request.getSession();
if (name.equals("dgy")&&password.equals("123")){
System.out.println("login Successfully");
session.setAttribute("username",name);
session.setAttribute("password",password);
request.getRequestDispatcher("LoginSuccess.jsp").forward(request, response);
}else{
System.out.println("Oops!!! pls correct");
response.sendRedirect("Login.jsp");
}
}

Third Step:

Creat another jsp: LoginSuccess.jsp

<body>
    This is my JSP page. <br>
    Username:${username}
    Password:${password}
</body>