2012-04-20 69 views
0

我已设置登录页面login.jsp。每当我去login.jsp并指定其他查询字符串,如如何在登录后在重定向的URL中保留查询字符串?

的login.jsp?你好

,点击登录,我重定向到index.jsp?hello查询字符串被剥离。

有没有办法保持查询字符串?

+0

您的“login.jsp”的内容可以吗? – Dave 2012-04-20 21:41:35

+0

这部署的服务器是什么? – 2012-04-21 03:19:13

回答

1

如果您使用容器管理的身份验证,那么这是不可能的。只有当查询字符串实际上位于,受限制的URL中时,它才会自动处理,最终用户首先显示登录页面。该容器将在成功登录后自动将重定向回受限制的URL,并填写原始查询字符串。

如果您使用的是本地认证,那么您只需将当前查询字符串复制到登录URL中即可。例如。

<form action="login?${pageContext.request.queryString}" method="post"> 
    <input type="text" name="username" /> 
    <input type="password" name="password" /> 
    <input type="submit" value="login" /> 
</form> 

然后,在servlet的doPost(),或任何你正在处理的登录,然后只是HttpServletRequest.getQueryString()抢查询字符串,并通过它传递重定向。例如。

User user = userService.find(username, password); 

if (user != null) { 
    request.getSession().setAttribute("user", user); // Do whatever you need to represent a logged-in user. 
    response.sendRedirect(request.getContextPath() + "/index.jsp?" + request.getQueryString()); 
} 
else { 
    // Show "unknown login" error? 
} 

所有的一切,我不知道有在登录网址查询字符串的这一功能要求如何有意义的真实世界。如果该查询字符串是您首先需要先登录的原始受限制URL的一部分,这与标准容器管理的身份验证机制支持的情况完全相同,那么这会更有意义。所以也许你需要重新考虑这个问题。

相关问题