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);
}
}
来源:oschina
链接:https://my.oschina.net/u/2510566/blog/680903