java 当用户未使用 Servlet 登录时如何重定向到索引页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11430216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:05:43  来源:igfitidea点击:

How to redirect to index page when user is not logged using Servlet?

javasessionservlets

提问by Java Questions

how to redirect to a login page if the user is not logged in using servlet.

如果用户未使用 servlet 登录,如何重定向到登录页面。

I have a login page user logs in using that and goes to home page,after navigating the user clicks on logout and just hits on back button in browser now the page goes to previously visited page, how to redirect the user to login page when using servlet?

我有一个登录页面用户使用它登录并转到主页,在导航用户单击注销并点击浏览器中的后退按钮现在页面转到以前访问过的页面,如何在使用时将用户重定向到登录页面小服务程序?

thanks for the help and continued support

感谢您的帮助和持续支持

回答by CodeNotFound

Use the following code in your jsp at the top of the page

在页面顶部的jsp中使用以下代码

if(userName.length()==0 || userType.length()==0 ){
        session.setAttribute("message", "Please Login");
         response.sendRedirect(response.encodeRedirectURL(path+"/"+destination));
    }

回答by 9ine

Use servlet ,

使用 servlet ,

In web.xml

在 web.xml 中

 <filter>
    <filter-name>AuthenticationFilter</filter-name>
    <filter-class>com.java.web.authentication.AuthenticationFilter</filter-class>
 </filter>
 <filter-mapping>
    <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/faces/manage/*</url-pattern>
  </filter-mapping>

In authentication filter ,

在身份验证过滤器中,

    if (visit == null) {
            servletContext.log("Redirecting to the login page.");
            session.setAttribute(Constants.ORIGINAL_VIEW_KEY, requestPath);
            String loginView = httpRequest.getContextPath() +  
            httpRequest.getServletPath() +Constants.LOGIN_PAGE;
            httpResponse.sendRedirect(loginView);
    } else {
            filterChain.doFilter(servletRequest, servletResponse);
    }

回答by Ramesh PVK

Ideally you should be doing this in Filter.

理想情况下,您应该在过滤器中执行此操作。

public void doFilter(.......){
      if(user logged in){
          filterChain.doFilter(...);
      } else {
        response.sendRedirect("/login.jsp");
      }
}

I would suggest you to use Servlet Security Featurewhich allows the container to care of Web Application Security.

我建议你使用Servlet Security Feature它允许容器照顾Web Application Security.

回答by amicngh

You can validate user session in Servletand if session is invalid or session doesn't contain userId(if at the time of session Creation you set that in session Attribute ) then redirect it to loginpage. to disable back you need to clear the cache using following code in jsp/servlet.

您可以验证用户会话,Servlet如果会话无效或会话不包含 userId(如果在会话创建时您在会话 Attribute 中设置了它),然后将其重定向到login页面。要禁用返回,您需要使用以下代码清除缓存jsp/servlet

response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store"); 
response.setHeader("Pragma","no-cache"); 
response.setDateHeader ("Expires", 0);

回答by Tomer

In your servlet doGet or doPost (whichever is called) you need to check if the user is logged in (I'm guessing you have the session checking mechanism) and if you see that the user is not logged in just use:

在您的 servlet doGet 或 doPost(以调用者为准)中,您需要检查用户是否已登录(我猜您有会话检查机制),如果您看到用户未登录,只需使用:

response.sendRedirect("/login.jsp");