繁体   English   中英

会话处理在 JSP 页面中不起作用?

[英]Session Handling is Not working in JSP Page?

当我提交 Index.jsp 中的表单时,它会转到 Home.jsp 页面,如果注销,它还会重定向到 Index.jsp 页面。 但是当我尝试在没有登录的情况下访问 Home.jsp 页面时显示错误。 理想情况下,它应该重定向到 Index .jsp 页面,因为 session at


主页.jsp

<%@page import="com.bean.UsersBean"%>
<%@page import="com.util.DAO"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="JS/General.js"></script>
<script
    src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

<link rel="stylesheet"
    href="https://use.fontawesome.com/releases/v5.1.1/css/all.css"
    integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ"
    crossorigin="anonymous">
<link rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" href="CSS/General.css" content="text/css">

<title>Insert title here</title>
</head>
<body style="background-color: grey">
<%@include file="sessionManagement.jsp"%>
    <%
            String email =session.getAttribute("email").toString();
            String password =session.getAttribute("pwd").toString();
            System.out.println(email + " JSP");
            System.out.println(password + " JSP");
            UsersBean obj = DAO.login(email, password);
            //System.out.println(obj.getProfile()); 
    %>

在此之后,我使用这个obj在页面中显示用户的一些详细信息。


sessionManagement.jsp 页面代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
<% if(session==null && session.getAttribute("email")==null)
        response.sendRedirect("Index.jsp");

    %>  
</body>

jsp 页面中的 if 语句有问题。

if(session == null && session.getAttribute("email") == null)

根据您的 if 语句,如果session为空,页面将被重定向,并且您在会话为空时检查属性,并且您没有在sessionManagement.jsp任何地方提到变量session

要记住的另一件事是,在jsp 页面中, HttpSession永远不会为null


替换您的 if 语句。

if(request.getSession().getAttribute("email") == null){
    response.sendRedirect("index.jsp");
    return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM