一、JSP跳转到Servlet
1、相对路径,如href="servlet/TestServlet"
如果写成"/servlet/TestServlet"会报错,因为第一个“/”表示的是【服务器根目录】
2、绝对路径,通过内置成员变量path实现,如href="<%=path%>/servlet/TestServlet"
path得到的是项目根目录,如【http://localhost:8080/ServletDemo】
二、Servlet跳转JSP
1、请求重定向:response.sendRedirect(request.getContextPath()+"/xxx.jsp");
request.getContextPath()获得项目根目录,或者通过"../xxx.jsp"取得上层路径得到
2、服务器内部转发:
request.getRequestDispatcher("../xxx.jsp").forward(req,resp);
request.getRequestDispatcher("/test.jsp").forward(request, response); //斜线表示项目的根目录
小结:Servlet都可以通过../xxx.jsp获取路径
三、web.xml的路径
web.xml的<url-pattern>必须以“/”开头,“/”表示项目的根目录
新建web工程ServletPathDirectionByMe
新建servlet包
新建Servlet:HelloServlet、TestServlet
生成web.xml、index.jsp
新建test.jsp
HelloServlet.java
doget()执行dopost()方法,dopost()输出界面。
package servlet;
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 HelloServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h1>您好,我是HelloServlet!</h1>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
TestServlet.java
package servlet;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 {/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//请求重定向方式跳转到Test.jsp 当前路径是ServletPathDirectionByMe/Servler///response.sendRedirect("text.jsp");错误//使用request.getContextPath()获得上下文对象//response.sendRedirect(request.getContextPath()+"/test.jsp");//服务器内部跳转,这里的/表示项目的根目录//request.getRequestDispatcher("/test.jsp").forward(request,response);request.getRequestDispatcher("../test.jsp").forward(request,response);}}
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>HelloServlet</servlet-name> <servlet-class>servlet.HelloServlet</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <!-- url-pattern处必须以/开头,这里的/表示项目的根目录 --> <url-pattern>/servlet/HelloServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/servlet/TestServlet</url-pattern> </servlet-mapping></web-app>
index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%><%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 'index.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> <h1>Servlet路径跳转</h1> <hr> <!-- 使用相对路径访问HelloServlet! --> <!-- /servlet/HelloServlet 第一个/表示服务器的根目录 --> <a href="servlet/HelloServlet">访问HelloServlet! </a><br> <!-- 使用绝对路径访问HelloServlet!可以使用path变量:path变量表示项目的根目录 --> <a href="<%=path %>/servlet/HelloServlet">访问HelloServlet! </a><br> <!-- 表单中action的URL地址写法,与超链接方式完全相同 --> <a href="servlet/TestServlet">访问TestServlet,跳转到Test.jsp</a> </body></html>
test.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%><%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 'index.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> <h1>Test.jsp</h1> <hr> </body></html>