• getServletContext()找不到这个路径


    我的一开始的源码是:

    public class BankInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            
             String username =  (String)request.getSession().getAttribute("adminusername");   
                if(username == null){
                   
                   response.sendRedirect(request.getServletContext().getContextPath()+"/admin/login");
                    return false;  
                }else  
                    return true;     
        }

    下面是报错:

    方法一:

    getServletContext().getContextPath() 没有这个方法当然报错了。
    你可以用request.getContextPath();

    public class BankInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            
             String username =  (String)request.getSession().getAttribute("adminusername");   
                if(username == null){
                   
                   response.sendRedirect(request.getServletContext().getContextPath()+"/admin/login");
                    return false;  
                }else  
                    return true;     
        }

    方法二:

    在一开始的基础上加上.getSession()

    public class BankInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            
             String username =  (String)request.getSession().getAttribute("adminusername");   
                if(username == null){
                   
                   response.sendRedirect(request.getSession().getServletContext().getContextPath()+"/admin/login");
                    return false;  
                }else  
                    return true;     
        }
  • 相关阅读:
    如何在Ubuntu 20.04 LTS Focal Fossa上安装Jekyll
    如何在CentOS 8 / RHEL 8服务器上安装qBittorrent
    如何在Kubernetes中向节点添加或删除标签
    java--io
    hdfs文件上传下载--client模式--Kerberos认证
    java--正则表达式使用
    Javaweb访问Hdfs--Kerberos认证
    Kerberos常用命令
    druid配置详解
    dubbo详细介绍
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/12132802.html
Copyright © 2020-2023  润新知