简体   繁体   English

.jsp中的登录系统

[英]Login system in .jsp

I have a login.jsp page which contains a login form. 我有一个login.jsp页面,其中包含一个登录表单。 Once logged in the user will be taken to index.jsp and this index.jsp should know which user is logged in. If the user refreshes the page he will stay logged in and not taken back to the login.jsp . 登录后,该用户将被带到index.jsp并且该index.jsp应该知道哪个用户已登录。如果用户刷新页面,则他将保持登录状态,而不会返回到login.jsp So there need to be some sort of session. 因此,需要进行某种形式的会话。

UPDATE Session Management UPDATE会话管理

Once the user has logged in you should add something to the session, such as the user name, to indicate the user is logged in. 用户登录后,您应在会话中添加一些内容,例如用户名,以表明该用户已登录。

You might then like to add a servlet filter that detects whether a request is coming from someone who is logged on by checking for a user name on the session. 然后,您可能想添加一个Servlet过滤器,该过滤器通过检查会话中的用户名来检测是否来自登录用户的请求。 If the person is not logged on your filter can send the request to your login.jsp instead of the actual page they requested. 如果该人未登录,则您的过滤器可以将请求发送到您的login.jsp而不是他们请求的实际页面。 Using a filter like this means you don't have to write any logon detection and redirecting in your JSP pages. 使用这样的过滤器意味着您不必在JSP页面中编写任何登录检测和重定向。

Finally you might want to provide a logout option which kills the session with session.invalidate(); 最后,您可能想提供注销选项,该选项可以使用session.invalidate();会话session.invalidate();

Use request.getSession() to begin a new session. 使用request.getSession()开始新的会话。 There you can save(using setAttribute method) your own Java objects which will be there during the whole session. 您可以在其中保存(使用setAttribute方法)您自己的Java对象,该对象将在整个会话期间保存在那里。

This can be done by using the session object: 这可以通过使用会话对象来完成:

<%
   String name = "testme";
   session.setAttribute( "theName", name ); //write as an attribute in the session object
%>

and some time later you can do: 稍后,您可以执行以下操作:

<% String name= session.getAttribute("theName")%> // retireve the attribute from the session

check here for a simple introduction: http://www.jsptut.com/Sessions.jsp 在此处查看简单介绍: http : //www.jsptut.com/Sessions.jsp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM