2016-02-05 87 views
2

我试图让用户在登录页面后登录;我在使用jsp页面(没有servlet,它是一个大学项目),没有DB的Apache Tomcat。保持用户使用jsp登录

我的问题是,登录页面后,用户有正确的用户名和密码(我验证这与<%System.out.println(usernameUser + " " + passwordUser); %>在新的页面)。 在这个新的页面我有一个新的表格,用户可以提交其他信息(obv不同于他的用户名和密码);这个提交是在这个页面上,但是在这个用户名和密码变成“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")); 
+2

不完全是一个好的做法,但罚款的学校项目。 http://www.tutorialspoint.com/jsp/jsp_session_tracking.htm ...保存** session **中的内容。每一个命中jsp页面检查会话对象 –

+0

是的,使用request.getSession()。setAttribute(“loginName”,loginName)或类似的。 –

+0

谢谢。 问题是,他从“新字符串(”blabla“);”而不是来自“request.getParameter(..)”。 我可以尝试,但我不知道如何适合我的代码,你能建议我一些吗? – bigghe

回答

0

你基本上会有类似的东西,你会检查该用户名这一

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

<% 
//save just user name in session 
request.getSession().setAttribute("userName", request.getParameter ("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 
+0

好吧,这似乎是我所做的: “session.setAttribute(”username“,request.getParameter(”username“)); “ 但可能我必须使用你的代码吗? 在此之后,我是否使用此参数使用“jsp:setProperty blabla”? – bigghe

+0

是的,只需将该字段添加到proprietarioBean类中,并检查userName是否为空。或者,检查您的jsp页面中的会话变量是否为空。 –

0

我的建议是存储用户和在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[] 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你可以找到的官方文件和有关的更多信息饼干