用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>


————————————————
版权声明:本文为CSDN博主「吃瓜群众欢乐多」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_36146776/article/details/60349599

posted @ 2021-05-23 17:42  云上看景  阅读(3346)  评论(0)    收藏  举报