1. 访问 WebContent 目录下的 JSP 文件
在 WebContent 目录下的文件可以直接在浏览器中访问。新建一个 test.jsp 文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body><% int value = 100; %><h1>value is <%= value %></h1> </body> </html>
访问结果:
2. 访问 Servlet 需要配置 web.xml
新建一个类并继承 HttpServlet 或直接新建一个 Servlet
package indi.tracine.servlet.test;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html; charset=UTF-8");PrintWriter out = response.getWriter();out.print("This is TestServlet");}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//doGet(req, resp); } }
在 web.xml 中配置该 Servlet 的 <servlet> 节点和 </servlet-mapping> 节点
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><display-name>jee-proj</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>TestServlet</servlet-name><servlet-class>indi.tracine.servlet.test.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/test</url-pattern></servlet-mapping> </web-app>
访问结果:
3. 访问 WEB-INF 目录下的 JSP 文件
WEB-INF是一个安全目录,客户端请求是无法访问该目录了下的文件,需要配置 web.xm l或利用 Servlet 跳转到该 jsp 文件
方法 1). 在 web.xm l中配置 WEB-INF 目录下的 jsp(output.jsp) 文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><display-name>jee-proj</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>output</servlet-name><jsp-file>/WEB-INF/output.jsp</jsp-file></servlet><servlet-mapping><servlet-name>output</servlet-name><url-pattern>/output</url-pattern></servlet-mapping> </web-app>
方法 2). 访问某个 Servlet,在 Servlet 内部转发到 jsp(output.jsp)文件(不是重定向)
package indi.tracine.servlet.test;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class OutputServlet extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/output.jsp").forward(request,response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//doGet(req, resp); } }