• 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;     
        }
  • 相关阅读:
    SQLPrompt 最新下载和使用方式
    ThinkPHP模板引擎
    Ajax随笔
    php生成mysql数据库dateTime类型时间
    json变量声明
    MYSQLI:mysqli预处理语句
    php网页乱码问题
    详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
    PHP给图片添加水印
    php中var关键字用法
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/12132802.html
Copyright © 2020-2023  润新知