JSP登录中Session的用法

本文介绍了一个使用Java Servlet实现的登录页面,包括账号密码输入验证,Session管理和登录后的跳转。重点在于演示如何在Servlet中处理表单提交,设置Session并根据输入验证跳转至不同页面。

转载:https://blog.youkuaiyun.com/qq_38006520/article/details/81514999

登录页面

<%@ 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>Insert title here</title>
</head>
<body>
    <div style="float:left;margin-top:100px;margin-left:200px;width:400px;height:300px;background:gray;">
        <form action="IndexServlet" method="post">
        <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px">
            <div style="margin-left:70px;float:left;line-height:30px">账号:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="user"/>
        </div>
        <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px">
            <div style="margin-left:70px;float:left;line-height:30px">密码:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="password"/>
        </div>
        <div style="float:left;margin-top:50px;width:400px;height:30px;background:gray;">
            <input style="float:left;width:60px;height:30px;margin-left:170px;border:none;" type="submit" name="ok" value="登录"/>
        </div>
        </form>
    </div>
</body>
</html>

检测账号密码以及设置session的IndexServlet

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
 
/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        
        String path = request.getContextPath();
        HttpSession session=request.getSession();
        
        if ("1".equals(user) && "1".equals(password)) {
            
            session.setAttribute("name", user);
            response.sendRedirect(path + "/success.jsp");
            
        }else{
            response.sendRedirect(path + "/Index.jsp");
        }
    }
 
}
成功登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    String path = request.getContextPath();
%>
<%
    Object name = session.getAttribute("name");
    if(name==null){
        response.sendRedirect(path+"/Index.jsp");
    }
%>
<html>
    <head>
        <title>成功页面</title>
    </head>
    <body>
        恭喜你,骚年,<%=session.getAttribute("name") %>,成功登陆了!
        <a href="out.jsp">注销</a>
    </body>
</html>

注销功能的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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% 
        String path = request.getContextPath();
    %>
    <%
           session.removeAttribute("name");
           response.sendRedirect(path+"/Index.jsp");
    %>
</body>
</html>

<br>JSP Login.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Login.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><form action=Login.jsp method="POST" ><br>Login Name: <input type="text" name="Name"><br><br>Login Password: <input type="text" name="Password" ><br><br><input type="submit" value="Send"><br><br><form><br><br><% if (request.getParameter("Name") != null &&<br> request.getParameter("Password") != null) { <br>String Name = request.getParameter("Name");<br>String Password = request.getParameter("Password");<br><br>if (Name.equals("mike") && Password.equals("1234")) { <br>session.setAttribute("Login", "OK");<br>response.sendRedirect("Member.jsp");<br>}<br>else { <br>out.println("登录错误,请输入正确名称"); <br>} <br>}<br>%><br><br></body><br></html> <br><br><br>JSP Member.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Member.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><% <br>String Login = (String)session.getAttribute("Login");<br><br>if (Login != null && Login.equals("OK")) { <br>out.println("欢迎进入");<br>session.invalidate(); <br>} <br>else { <br>out.println("请先登录,谢谢") ;<br>out.println("<br>经过五秒之后,网页会自动返回Login.jsp");<br><br>response.setHeader("Refresh","5;URL=Login.jsp"); <br>}<br>%><br><br></body><br></html> <br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值