首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JSP-ServLet 一个容易的密码核对

2012-12-27 
JSP-ServLet 一个简单的密码核对First Step:Create One Jsp file: Login.jspbodyThis is my JSP page.

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>

热点排行
Bad Request.