---
title: "Servlet路径跳转"
author: "cakin24"
date: "2017-10-14T09:01:09Z"
tags: []
categories: ["编程语言"]
slug: "servlet"
draft: false
lastmod: "2017-10-14T09:01:09Z"
id: 2396269
url: "https://cakin24.iteye.com/blog/2396269"
---

一 绝对路径：放之四海而皆准的路径。

 

二 相对路径：相对于当前资源的路径。

 

三 代码

1、index.jsp

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'index.jsp' starting page&lt;/title&gt;

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;Servlet路径跳转&lt;/h1&gt;

    &lt;hr&gt;

    &lt;!--使用相对路径访问HelloServlet --&gt;

    &lt;!-- /servlet/HelloServlet 第一个/表示服务器的根目录 --&gt;

    &lt;a href="servlet/HelloServlet"&gt;使用相对路径访问HelloServlet!&lt;/a&gt;&lt;br&gt;

    &lt;!-- 使用绝对路径 访问HelloServlet,可以使用path变量:path变量表示项目的根目录--&gt;

    &lt;a href="&lt;%=path%&gt;/servlet/HelloServlet"&gt;使用绝对路径访问HelloServlet!&lt;/a&gt;&lt;br&gt;

    &lt;!--表单中action的URL地址写法，与超链接方式完全相同。 --&gt;

    &lt;a href="servlet/TestServlet"&gt;访问TestServlet,跳转到Test.jsp&lt;/a&gt;

    

  &lt;/body&gt;

&lt;/html&gt;

2、web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="2.5"

        xmlns="[http://java.sun.com/xml/ns/javaee"](http://java.sun.com/xml/ns/javaee%22);

        xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);

        xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)

[http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd%22);&gt;

  &lt;display-name&gt;&lt;/display-name&gt;

  &lt;servlet&gt;

    &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;

    &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;

    &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;

    &lt;servlet-class&gt;servlet.HelloServlet&lt;/servlet-class&gt;

  &lt;/servlet&gt;

  &lt;servlet&gt;

    &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;

    &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;

    &lt;servlet-name&gt;TestServlet&lt;/servlet-name&gt;

    &lt;servlet-class&gt;servlet.TestServlet&lt;/servlet-class&gt;

  &lt;/servlet&gt;

 

 

  &lt;servlet-mapping&gt;

    &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;

    &lt;!--url-pattern处必须以/开头，这里的/表示项目的根目录  --&gt;

    &lt;url-pattern&gt;/servlet/HelloServlet&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;

  &lt;servlet-mapping&gt;

    &lt;servlet-name&gt;TestServlet&lt;/servlet-name&gt;

    &lt;url-pattern&gt;/servlet/TestServlet&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;       

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

&lt;/web-app&gt;

3、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 {

    /\**

     * Constructor of the object.

     \*/

    public TestServlet() {

        super();

    }

    /\**

     * Destruction of the servlet. &lt;br&gt;

     \*/

    public void destroy() {

        super.destroy(); // Just puts "destroy" string in log

        // Put your code here

    }

    /\**

     * The doGet method of the servlet. &lt;br&gt;

     *

     * 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. &lt;br&gt;

     *

     * 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,当前路径是ServletPathDirection/servlet/，所有下面这句不对

         //response.sendRedirect("test.jsp");

         //使用request.getContextPath获得上下文对象

         //response.sendRedirect(request.getContextPath()+"/test.jsp");

    

        //服务器内部跳转,这里的斜线表示项目的根目录

        //request.getRequestDispatcher("/test.jsp").forward(request, response);

        request.getRequestDispatcher("../test.jsp").forward(request, response);

    }

    /\**

     * Initialization of the servlet. &lt;br&gt;

     *

     * @throws ServletException if an error occurs

     \*/

    public void init() throws ServletException {

        // Put your code here

    }

}

 

四 测试

![](http://dl2.iteye.com/upload/attachment/0127/3677/5f9ba26d-4be6-36d2-b040-726112d535cd.png)