Tomcat中<url-pattern>映射注解和<form action>的区别by菜鸟风闲MniYyg

regist.html的代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>httpservlet请求</title>
</head>
<body>
<h2>get请求</h2>
<form action="/helloServlet" method="get">                  //  #1
    <input type="submit" value="提交get请求">
</form>1

<h2>post请求</h2>
<form action="helloServlet" method="post">                  //  #2
    <input type="submit" value="提交post请求">
</form>
</body>
</html>

HelloServlet配置如下:

package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/helloServlet")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("抓到Post请求");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("抓到Get请求");

    }
}

xml配置如下,但其实这里的xml是多余的,因为已经加了注解映射路径@WebServlet("/helloServlet")了。但是设置的hello也能访问。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.itheima.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

资源路径为:/diy
当用户访问:localhost:8080/diy/register.html时,正常访问到网页

在这里插入图片描述

当用户点击post请求时,tomcat正常捕捉下post请求,跳转的url地址为:http://localhost:8080/diy/helloServlet
当用户点击get请求时,tomcat无法捕捉到get请求,并且跳转url地址为:http://localhost:8080/helloServlet?,同时网页404报错
对比下,发现get请求的时候,url少了个资源路径/diy,为什么呢?
是这样的:
@WebServlet("/helloServlet") 这个是映射路径,映射路径加/,是语法要求,不能删除这个/,如果你删除了,会出现语法问题,重启服务器后,tomcat直接报错无法连接上tomcat。
在这里插入图片描述

在这里插入图片描述

<form action="helloServlet">,这里也是固定语法要求,当你这么写,那么代表,当用户在网页上点按钮提交了请求后,请求的数据回传给服务器的时候,告知服务器找哪个Web资源的Servlet来处理,服务器自动解析为:http://localhost:8080/diy/helloServlet。这里你可以理解为是虚拟路径,代表了编辑了一个地址,不真实存在,但是指向并标记了一个地址。
但是当你<form action="/helloServlet"> 这么写的时候,这时候点提交按钮,url跳转地址为:http://localhost:8080/helloServlet?,为什么呢,因为服务器认为你是给了一个真实地址,因此直接就跳转过去,但是不符合要求,因此报404错误。如果你非要加/,应该修正为:<form action="/diy/helloServlet"> ,这样点提交就可以正确跳转了
那如何区分get和post呢,一个是在html中method名我们有所区分,还有一个是我们没有必要区分,service中关于http请求的有7种方式,put/get/post/delete啥的,但是和表单相关的只有post和get,所以我们只处理他俩就可以了。

在这里插入图片描述

web.xml文件的<servlet-pattern> 和 注解配置路径的@WebServlet("/helloServlet"),原则上,我们保持一致,因为这两个都代表跳转的wbe资源中的Servlet类,像本次资料中,
<servlet-pattern>/hello</servlet-pattern>,访问地址是:http://localhost:8080/diy/hello,提交url请求后HelloServlet可以正常抓取到请求。而pattern是可以添加多个的,访问了可以共享。例如下图,下面网址hello1和hello2和hello3都是可以访问的。

http://localhost:8080/diy/hello1
http://localhost:8080/diy/hello2
http://localhost:8080/diy/hello3

@WebServlet("/helloServlet"),访问地址是:http://localhost:8080/diy/helloServlet,提交url请求后HelloServlet也可以正常抓取到请求。但是注意映射注解,无法添加多个哈,只能添加一个。

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值