/WEB-INF路径下访问方法

匆匆过客 提交于 2020-03-01 15:46:21

package cn.meeting.core;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 执行转发用的
 *
 *1 区别

假定你的web application 名称为qlumeeting,你在浏览器中输入请求路径:

http://localhost/qlumeeting/login.html

1.1 System.out.println(request.getContextPath());

打印结果:/qlumeeting

1.2 System.out.println(request.getServletPath());

打印结果:/login.html

1.3 System.out.println(request.getRequestURI());

打印结果:/qlumeeting/login.html

1.4 System.out.println(request.getRealPath("/"));

打印结果:F:\Tomcat 6.0\webapps\news\test
 *
 */

public class ForwardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contextPath =  request.getContextPath();//  /qlumeeting
        String uri = request.getRequestURI();//  /qlumeeting/login.html
        
        uri = uri.replace(contextPath,"");// /login.html
        String path = "/WEB-INF"+uri;//   /WEB-INF/login.html
        System.err.println(path);
        request.getRequestDispatcher(path).forward(request, response);
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!