需要登录Servlet才能转到其他页面 [英] Require login servlet to go to other pages
问题描述
网络上有数百万个示例,这些示例显示了如何创建基本的Java登录Servlet,用户必须在其中输入用户名和密码,并通过某些数据库对其进行验证.登录信息正确时,它将您重定向到另一个页面.
There are a million examples on the web that shows how to create a basic Java Login Servlet, where the user has to enter a username and password which gets verified by some database. When the login info is correct, it redirects you to another page.
问题在于,这些教程仅教如何验证用户是否输入了正确的用户名/密码.如果用户知道其他页面的URL,则可以完全跳过整个登录过程.我该如何做才能使我的登录servlet实际登录到我,并且需要它才能访问其他页面?
The problem is that the tutorials only teach how to verify if a user has inputted the correct username / password. If the user knew the URL to the other pages, he can skip the entire login process altogether. How do I make it so my login servlet actually logs me in and is required to access the other pages?
这是我当前的登录过程
if(user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("Unit_Info.jsp");
}else
response.sendRedirect("http://google.com");
推荐答案
从用户那里获取登录详细信息之后,您可以将登录详细信息保存在会话中,并且可以在每个页面中测试或验证会话,例如...
After getting the login details from the user, you can save the login details in session and in every page you can test or validate the session like...
<%
String strLoginId = (String)session.getAttribute("LoginID");
String strpassword = (String)session.getAttribute("Password");
if(strLoginId==null) // Means session is not there
%>
<jsp:forward page="Error.jsp" />
这篇关于需要登录Servlet才能转到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!