用jsp实现登录,登录成功则跳转到登录成功页面,失败则跳转到失败页面

login.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<div>
		<form action="dologin.jsp" method="post">
			<span>用户名</span>
			<input type="text" name="username" />
			<span>密码</span>
			<input type="text" name="password" />
			<input type="submit" value="登录"/>
		</form>
	
	</div>
</body>
</html>


dologin.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
	String path=request.getContextPath();
	String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+path+"/";
	String username="";
	String password="";
	request.setCharacterEncoding("utf-8");
	username=request.getParameter("username");
	password=request.getParameter("password");
	out.println(username);
	out.println(password);
	if("admin".equals(username) && "admin".equals(password)){
		session.setAttribute("loginUsername", username);
		session.setAttribute("loginUser",username);
		request.getRequestDispatcher("login_success.jsp").forward(request,response);
	}else{
		response.sendRedirect("login_failure.jsp");
	}	
%>

login_success.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<div>
		欢迎<h3><%=session.getAttribute("loginUsername") %></h3>,登录成功!!!!
	</div>
</body>
</html>

login_failure.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<div>
		登录失败,请检查用户名或者密码!!!
		<a href="login.jsp">login</a>
	</div>
</body>
</html>



©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页