2016-03-31 67 views
-1

我正在用JSP创建一个工具。 我有多个JSP页面从用户接收输入。 现在我想在许多其他jsp页面上使用该输入。我想从JSP页面获取值到多个jsp页面。可能吗?

假设我有登录页面作为我的第一页当用户输入用户名我能够得到它在我的下一个jsp页面,我用Form方法作为后置动作。 但我无法在其他Jsp页面上显示相同的用户名。

我使用JSP:的getProperty用于获取值:

<jsp:getProperty property="username" name="user2"/><br> 
<jsp:getProperty property="password" name="user2"/><br> 

请建议有什么办法让用JSP在多个页面上的值:useBean的

+2

将这些值存储在会话中。 – Thomas

回答

0
<% HttpSession httpSession = request.getSession(); 
      httpSession.setAttribute("user2", user2); 
    %> 

    then from any jsp page u can access that attribute for same session like this 

    <%HttpSession httpSession=request.getSession(); 
     httpSession.getAttribute("user2"); 
    %> 
+0

我试图使用它作为 但它不起作用。与范围=“页面”它至少为下一页工作。 – shilpa

+0

尝试我已更新的代码现在检查出来 – Sumeet

+0

<%HttpSession httpSession = request.getSession(); httpSession.setAttribute(“user2”,user2); %> 这需要在servlet中加入吗? – shilpa

-1

是的,你可以访问它以这样的方式

的login.jsp:

<form action ="process.jsp"> 
    Username: 
    <input type="text" name="username"> 
    Password: 
    <input type="password" name="password"> 
<input type="submit" value="Submit"> 
    </form> 

process.jsp:

<% String username = request.getParameter("username"); 
     String password = request.getParameter("password"); 
    %> 
+0

是的,但只有在process.jsp中,如果我想在多个jsp页面上使用相同的用户名密码,该怎么办? – shilpa

+0

那么你必须使用会议,帮助你。 –

0

是的,我们可以通过会话传递价值做到这一点。如下所示request.getSession().setAttribute("key", <value>);

+0

好的。我会尝试使用这个选项。 – shilpa

0

您可以将bean存储在会话中,以便可以从JSP直接访问它。 (见this answer on SO)。

你的bean声明应该是这样的:

<jsp:useBean id="user2" class="mycompany.User" scope="session" /> 

如果你想真正持久的数据,你应该存储在数据库中。

请注意,您应该考虑MVC框架,例如Spring MVCApache Struts,但您将不得不同化大量的概念。

相关问题