网站上的JSP会话

时间:2018-08-19 14:15:58

标签: jsp

我有一个名为

的代码
User loggeduser = (User)session.getAttribute("loggedUser");

我对这段代码的使用感到困惑。任何人都可以解释此代码的用法吗?为什么以及如何在其他页面上使用?

1 个答案:

答案 0 :(得分:0)

JSP会话对象用于将一个参数从一个jsp页面发送到另一个。

假设您有一个index.html表单,该表单仅向用户询问其用户名。

<html>  
<body>  
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  
</body>  
</html> 

一旦用户单击“提交”按钮welcome.jsp页面将被加载

<html>  
<body>  
<%   

String name=request.getParameter("uname");  
out.print("Welcome "+name);  

session.setAttribute("user",name);  

<a href="second.jsp">second jsp page</a>  

%>  
</body>  
</html>  

现在我还有另一个页面想要知道已登录用户的名称 为此,我可以像这样使用session.getAttribute

<html>  
<body>  
<%   

String name=(String)session.getAttribute("user");  
out.print("Hello "+name);  

%>  
</body>  
</html>  
相关问题