WEB-INF文件夹下面的jsp页面是不能直接被访问的,因为应用服务器把WEB-INF指为禁访目录。
1.利用servlet进行访问
,如web-inf下有a.jsp则可以用request.getrequestdispatcher("/web-inf/a.jsp").forward(request,response);进行派遣访问.但如果web-inf下有a.htm,则用request.getrequestdispatcher("/web-inf/a.htm").forward(request,response);就不能访问.
至于原理的话,可以去看看Tomcat的源码。多多学习。
例如:访问WEB-INF文件夹下的register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <a href="<%=basePath%>zhuceServlet">注册</a> </body> </html>
zhuceServlet.java:
public class zhuceServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/jsp/register.jsp").forward(request,response); } }
这样,点击注册,就能访问WEB-INF文件夹下的jsp了
2.利用session访问webroot下的页面