当我从index.jsp登录时,我可以看到确认说登录成功但是如果按F5按钮则不考虑上一个会话并且创建新会话...所以我必须再次登录再次,如果我按F5 ...
如何使会话持久?
的index.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page session="true"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Practica3 </title>
<link rel="stylesheet" type="text/css" href="css/structure.css" />
<script type="text/javascript" src="jquery/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".menu").click(function(event) {
$('#content').load('Content',{content: $(this).attr('id')});
});
});
</script>
</head>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
This is the Header
</div>
<!-- End Header -->
<!-- Begin Navigation -->
<div id="navigation">
<jsp:include page="menu3.jsp" />
</div>
<!-- End Navigation -->
<!-- Begin Faux Columns -->
<div id="faux">
<!-- Begin Left Column -->
<div id="leftcolumn">
</div>
<!-- End Left Column -->
<!-- Begin Content Column -->
<div id="content">
<jsp:include page="login.jsp" />
</div>
<!-- End Content Column -->
<!-- Begin Right Column -->
<div id="rightcolumn">
</div>
<!-- End Right Column -->
</div>
<!-- End Faux Columns -->
<!-- Begin Footer -->
<div id="footer">
This is the Footer
</div>
<!-- End Footer -->
</div>
<!-- End Wrapper -->
</body>
</html>
引入了menu.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="false"%>
<script type="text/javascript">
$(document).ready(function() {
$(".menu").click(function(event) {
$('#content').load('Content',{content: $(this).attr('id')});
});
});
</script>
<%HttpSession session = request.getSession(true);
System.out.println("cargamos menu.jsp");
System.out.println(" (menu.jsp)Sesion:"+session);
System.out.println("Sesion(user):"+session.getAttribute("user"));
if ((session != null) && (session.getAttribute("user")!=null)) {
%>
<table>
<tr>
<td> <a class="menu" id="logout.jsp" href=#> Logout </a> </td>
</tr>
</table>
<% }
else {%>
<table>
<tr>
<td> <a class="menu" id="form.jsp" href=#> Registration </a> </td>
<td> <a class="menu" id="login.jsp" href=#> Login </a> </td>
</tr>
</table>
<%}; %>
的login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="models.BeanLogin" %>
<script type="text/javascript" src="jquery/jquery-1.7.1.js"></script>
<script type="text/javascript" src="jquery/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("#registerForm").validate({
submitHandler: function(form) {
$('#content').load('logincontroller',$("#registerForm").serialize());
}
});
}
);
</script>
</head>
<body>
<%
BeanLogin login = null;
if (request.getAttribute("login")!=null) {
login = (BeanLogin)request.getAttribute("login");
}
else {
login = new BeanLogin();
}
%>
<form id=registerForm action="/Practica3/logincontroller" method="POST">
<table>
<tr>
<td> User id </td>
<td> <input type="text" name="user" value="<%=login.getUser() %>" id="user" class="required" minlength="5"/> </td>
<%
if ( login.getError()[0] == 1) {
%>
<td class="error"> Invalid username or password. </td>
<%
}
%>
</tr>
<tr>
<td> Password </td>
<td><input type="password" name="password" placeholder="Password"
value="<%=login.getPassword()%>" id="password" class="required" minlength="8" /></td>
</tr>
<tr>
<td> <input name="submit" type="submit" value="Enviar"> </td>
</tr>
</table>
</form>
loginOk.jsp:
<script type="text/javascript">
$(document).ready(function() {
$('#navigation').load('menu.jsp');
});
</script>
Logged in!
loginController.jsp:
/**
* Servlet implementation class logincontroller
*/
public class logincontroller extends HttpServlet {
private static final long serialVersionUID = 1L;
private static DAO Dao;
/**
* @throws Exception
* @see HttpServlet#HttpServlet()
*/
public logincontroller() throws Exception {
super();
Dao = new DAO();
// 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
BeanLogin login = new BeanLogin();
BeanUtilities.populateBean(login, request);
try {
if (login.isComplete() && checkLogin(login)) {
HttpSession session = request.getSession();
session.setAttribute("user",login.getUser());
System.out.println("Se ha hecho el login."+session.toString());
System.out.println("User:"+session.getAttribute("user"));
session.setMaxInactiveInterval(10);
RequestDispatcher dispatcher = request.getRequestDispatcher("loginOk.jsp");
if (dispatcher != null) dispatcher.forward(request, response);
} else {
request.setAttribute("login",login);
RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
if (dispatcher != null) dispatcher.forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
protected boolean checkLogin(BeanLogin bean) throws SQLException{
String query = "SELECT id,password FROM ts1.users;";
ResultSet rs = Dao.executeQuerySQL(query);
while(rs.next()){
if (rs.getString(1).equals(bean.getUser()) && rs.getString(2).equals(bean.getPassword())){
return true;
}
}
int[] errors = bean.getError();
errors[0]++;
bean.setError(errors);
return false;
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Practica3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Content</display-name>
<servlet-name>Content</servlet-name>
<servlet-class>controllers.Content</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Content</servlet-name>
<url-pattern>/Content</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>formcontroller</display-name>
<servlet-name>formcontroller</servlet-name>
<servlet-class>controllers.formcontroller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>formcontroller</servlet-name>
<url-pattern>/formcontroller</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>logincontroller</display-name>
<servlet-name>logincontroller</servlet-name>
<servlet-class>controllers.logincontroller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logincontroller</servlet-name>
<url-pattern>/logincontroller</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>closesession</display-name>
<servlet-name>closesession</servlet-name>
<servlet-class>controllers.closesession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>closesession</servlet-name>
<url-pattern>/closesession</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>logoutcontroller</display-name>
<servlet-name>logoutcontroller</servlet-name>
<servlet-class>controllers.logoutcontroller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logoutcontroller</servlet-name>
<url-pattern>/logoutcontroller</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>-1</session-timeout>
</session-config>
</web-app>
答案 0 :(得分:0)
方法setMaxInactiveInterval()接受时间为秒。您将会话的最长非活动时间设置为10秒。将它设置为更多的时间。
答案 1 :(得分:0)
我假设您在loginController
转发的页面上按F5,该应该是loginOK.jsp
。你可能认为你刷新了页面loginOK.jsp
,这绝对不是。实际上,您刚刚刷新了loginController
servlet。
原因是,在loginController
中,您使用转发代替重定向。当loginOK.jsp
显示时,您的浏览器上的网址仍为loginController
而不是loginOK.jsp
。刷新此页面后,loginController
会刷新。因此,为loginController
创建了一个新的请求,创建了一个新的登录对象。当您在loginController
中检查/验证登录对象时,它将无法通过检查/验证。因此,loginController
通过您的login.jsp
语句转发至else
。
一种可能的解决方案是使用重定向而不是前进:
response.sendRedirect(...);
但是,我建议您按照MVC模式重新设计登录流程,不要将java代码段/代码放在jsp页面中。
祝好运。