Java web 6-1用户登录验证
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="loginCheck" method="post">
请输入用户名:<input type="text" name="username"/><br/>
请输入密码: <input type="password" name="userpwd"/><br/>
<input type="submit" value="登录"/>
<input type="reset"/>
</form>
</body>
</html>
package servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginCheckServlet6_1 extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String userName=request.getParameter("username");
String userPwd=request.getParameter("userpwd");
String info="";
if(("meng".equals(userName))&&("123456".equals(userPwd))) {
info="欢迎你"+userName+"! ";
}else {
info="用户名或密码不正确!";
}
request.setAttribute("outputMessage", info);
request.getRequestDispatcher("/Info6-1.jsp").forward(request, response);
}
}
<servlet>
<servlet-name>LoginCheckServlet6_1</servlet-name>
<servlet-class>servlets.LoginCheckServlet6_1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCheckServlet6_1</servlet-name>
<url-pattern>/loginCheck</url-pattern>
</servlet-mapping>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示结果页面</title>
</head>
<body>
<%=request.getAttribute("outputMessage") %>
</body>
</html>
运行
登录成功:
登录失败: