Servlet路径跳转 博客分类: java Servlet
程序员文章站
2024-03-18 16:35:10
...
一 绝对路径:放之四海而皆准的路径。
二 相对路径:相对于当前资源的路径。
三 代码
1、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>
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
<display-name></display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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,当前路径是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. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
四 测试
上一篇: 解决Cannot change version of project facet Dynamic web module to 2.5 博客分类: mavenservlet servletmaven
推荐阅读
-
web.xml 中的 servlet 和 servlet-mapping 标签 (转) 博客分类: sevrlet servlet
-
Servlet简介 博客分类: java Servlet
-
Tomcat装载Servlet的三种情况 博客分类: java TomcatServlet
-
Servlet3.1规范翻译 - 应用生命周期事件 博客分类: Tomcat servlet
-
【转载】web.xml出现中
default 的作用 博客分类: jsp&servlet servlet -
HttpServletRequest接口 博客分类: Servlet HttpServletRequest
-
简易购物车Servlet类实现 博客分类: java 简易购物车Servlet
-
Servlet获取表单数据 博客分类: java Servlet
-
Servlet3.1规范翻译 - Servlet Context 博客分类: Tomcat servlet
-
servlet3.1规范翻译 - 会话(session) 博客分类: Tomcat ServletSession