[英]Session Handling is Not working in JSP Page?
When I submit a form present in Index.jsp it goes to Home.jsp page and if log out it also redirects to Index.jsp page.当我提交 Index.jsp 中的表单时,它会转到 Home.jsp 页面,如果注销,它还会重定向到 Index.jsp 页面。 But when I try to access Home.jsp page without login is shows an error.
但是当我尝试在没有登录的情况下访问 Home.jsp 页面时显示错误。 Ideally it should redirect to Index .jsp page because session at
理想情况下,它应该重定向到 Index .jsp 页面,因为 session at
Home.jsp主页.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());
%>
After this I have used this obj to display some details of user in the page.在此之后,我使用这个obj在页面中显示用户的一些详细信息。
sessionManagement.jsp page code 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>
There's a problem with your if statement in the jsp page. jsp 页面中的 if 语句有问题。
if(session == null && session.getAttribute("email") == null)
according to your if statement, the page is redirected if the session
is null, and you are checking for an attribute when the session is null, and you haven't mentioned the variable session
anywhere in sessionManagement.jsp
.根据您的 if 语句,如果
session
为空,页面将被重定向,并且您在会话为空时检查属性,并且您没有在sessionManagement.jsp
任何地方提到变量session
。
Another thing to keep in mind is that in a jsp page the
HttpSession
will never benull
.要记住的另一件事是,在jsp 页面中,
HttpSession
永远不会为null
。
Replace your if statement.替换您的 if 语句。
if(request.getSession().getAttribute("email") == null){
response.sendRedirect("index.jsp");
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.