使用 jsp 保持用户登录

Keeping user logged in with jsp

我试图让用户在登录页面后保持登录状态;我在 Apache Tomcat 上使用 jsp 页面(没有 servlet,这是一个大学项目),没有数据库。

我的问题是,在登录页面后,用户拥有正确的用户名和密码(我在新页面中用 <%System.out.println( usernameUser + " " + passwordUser ); %> 验证了这一点)。 在这个新页面中,我有一个新表单,用户可以在其中提交其他信息(显然不同于他的用户名和密码);这个提交是在这个页面本身,但是在这个用户名和密码变成 "null".

之后

这里是代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<jsp:useBean id="proprietarioBean" scope="session" class="gestioneLocazioni.ProprietarioBean" />
<jsp:setProperty name="proprietarioBean" property="*" /> 

<%System.out.println( request.getParameter("username") + " " + request.getParameter("password") ); %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    [OTHER CODE]

    <form action="ProprietarioLogin.jsp" name="myform" method="POST">
    <table>
        <tr>
            <td>Nome locazione:</td>
            <td><input name="nomeLoc" id="nomeLoc" type="text"></td>                 
        </tr>
        <tr>
            <td>Nome proprietario della locazione:</td>
            <td><input name="nomeProp" id="nomeProp" type="text"></td>              
        </tr>

        <tr>    
            <td><input name="aggLoc" id="aggLoc" type="submit" value="Aggiunti locazione"> </td>
        </tr>
        <tr>
            <td>
            <%if( request.getParameter("aggLoc") != null){ 
                String nomeLoc = request.getParameter("nomeLoc");
                String nomeProp = request.getParameter("nomeProp");

                if(proprietarioBean.validateInsert(nomeLoc, nomeProp)){ 
                    System.out.println("Locazione inserita: " + request.getParameter("nomeLoc")); 
                    proprietarioBean.richiediInserimento(request.getParameter("nomeLoc"), request.getParameter("nomeProp"));>
                    <p><b>Locazione inserita:</b> <%=request.getParameter("nomeLoc")%> </p>
                    <%  int k = proprietarioBean.dimList(); 
                    System.out.println("k vale: "+ k);
                    for(int i = 0; i <= k; i++){
                        %><p> <%=proprietarioBean.getLocazionePend(i) %></p><%
                    }
                  }

              else{
                System.out.println("Dati errati"); %>
                   <p><strong>DATI ERRATI</strong></p><%  
                   }
            }%>
               </td>
        </tr>


    </table>
    </form>

如果需要,我可以附加登录代码。 提前谢谢你。


编辑 现在我可以像这样获得正确的用户名和密码:

String username = (String) session.getAttribute("username");           
String password = (String) session.getAttribute("password");
System.out.println( "Da session.getAttribute: " + username + " " + password +"\n"); 

现在我必须将此信息用于 jsp:setProperty ?

编辑 2:已解决!

感谢大家的回答。 我用这个解决了:

proprietarioBean.setUsername((String) session.getAttribute("username"));
proprietarioBean.setPassword((String) session.getAttribute("password"));

我的建议是将用户和密码加密存储在 cookie 中。

例如:

if(rememberMe){

Cookie c = new Cookie("userid", userId.toString());
c.setMaxAge(24*60*60);
response.addCookie(c);  // to add it to the response 

}

然后要读取 cookie,您可以这样做:

Cookie[] cookies = request.getCookies();     

boolean foundCookie = false;

for(int i = 0; i < cookies.length; i++)
{ 
    Cookie c = cookies[i];
    if (c.getName().equals("userid")){
        string userId= c.getValue();
        foundCookie = true;
    }
}  

Here你可以找到官方文档和更多关于cookies的信息

你基本上会有这样的东西

Login.jsp(处理登录的页面)

<%
//save just user name in session
request.getSession().setAttribute("userName", request.getParameter  ("username"));
 %>

在其他页面上,您将检查 userName 是否不为空

<%
if(request.getSession().getAttribute("userName") != null)
 //do something
else 
//do something else
%>

您可以通过

使会话无效
<% request.getSession().setAttribute("userName", null);%> //or
//just call the invalidate method on the session object