码迷,mamicode.com
首页 > 其他好文 > 详细

getServletContext()找不到这个路径

时间:2020-01-02 15:45:57      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:login   ret   方法   row   ati   gets   src   com   err   

我的一开始的源码是:

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;     
    }

getServletContext()找不到这个路径

标签:login   ret   方法   row   ati   gets   src   com   err   

原文地址:https://www.cnblogs.com/WLCYSYS/p/12132802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!