servlet路径详解(转)

来源:互联网 发布:删除网络用户名和密码 编辑:程序博客网 时间:2024/05/01 19:31

关键字: servlet路径详解

我同学小鲁对于 web.xml中配置的servlet url-pattern和jsp页面请求的action地址之间的关系搞不明白,我经过实验 总结如下:

假设我在com.servlet.test包下有一个 HelloServlet的Servlet
package com.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 HelloServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    PrintWriter pw = resp.getWriter();
    pw.write("<html><head></head><body>hello world</body></html>");
    pw.flush();
    pw.close();
    }
}


我在web.xml下配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ServletTest</display-name>

       <servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.servlet.test.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>hello</servlet-name>
    <!-- 这里的/是不能省略的,代表的含义是工程根目录,比如我的工程名字是  ServletTest 这里就代表http://localhost:8080/ServletTest 地址栏只要请求http://localhost:8080/ServletTest/helloworld 求可以进入这个servlet-->
  <url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

web下test文件包下面有一个index.jsp页面

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<!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>
<form action="../helloworld" method="get">
      <input type="submit" value="提交">
</form>
</body>
</html>

这里的action为什么是../helloworld呢? 因为该jsp页面是位于test文件下,也就是http://localhost:8080/ServletTest/test/index.jsp 而../后 目录变成了
http://localhost:8080/ServletTest/ 此时再加上helloworld 正好组成了xml中配置的
http://localhost:8080/ServletTest/helloworld 对应的servlet路径

如果我这里的action路径写成下面的形式 又会是什么情况呢?
<form action="/helloworld" method="get">
      <input type="submit" value="提交">
</form>
这个时候 我提交表单的时候 发现地址栏显示的路径是 :
http://localhost:8080/helloworld 也就是说这里的/表示的是域名的路径:
http://localhost:8080/
所以这里的 / 表示的意思和web.xml中的 / 表示的意思是不同的 :

前者表示 http://localhost:8080/

而web.xml的那个/表示: http://localhost:8080/ServletTest 也就是多带了个工程名字

所以上面的action改成:
<form action="/ServletTest/helloworld" method="get">
      <input type="submit" value="提交">
</form>
就没有问题了。

总结一下 : web.xml的 / 表示的是http://localhost:8080/ + 工程名
而jsp的action
加了 /的时候表示绝对路径http://localhost:8080/ 
而没有加 / 表示的是相对路径
原创粉丝点击