本例结构图
文章目录
maven项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Tomcat_App</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--tomcat plugin-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
用于配合servlet的html资源
修改form的action属性,分别对应各个servlet,修改method为GET,或POST,分别测试浏览器的GET,POST提交方式
本例各servlet基本测试方式就是访问html资源,提交html的表单到servlet,查看控制台输出
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test for Servlet</title>
</head>
<body>
<form action="/Tomcat_App/demoX" method="post">
<input name="username">
<input type="submit">
</form>
</body>
</html>
第一个servlet,此为测试init, service, destroy三个方法的调用情况
package com.diy.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet(urlPatterns = "/demo1", loadOnStartup = -1)
public class ServletDemo implements Servlet {
/**
* 1.Invoke timing, default when servlet be visited first time tomcat will invoke this method
* if the loadOnStartup property has been change from -1 to 1 or 0, no need visiting, init will be invoked
* 2.Invoke times, only be invoked once
* @param servletConfig
* @throws ServletException
*/
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("Servlet is initializing~~~~~");
}
/**
* 1.Invoke timing, every time be visited this method will be invoked
* 2.Invoke times, depends on how many times servlet be visited
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Here is the servlet service running~~~");
}
/**
* 1.Invoke timing, when memory be release or server close, Servlet object will be destroyed
* 2.Invoke times, only once
*/
@Override
public void destroy() {
System.out.println("Servlet has been destroyed");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
//you can also return a ""
}
}
第二个servlet,查看不同的html提交method,是否正常调用doGet和doPost
package com.diy.web;
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("/demo2")
public class ServletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("here is doGet~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("here is doPost~~~");
}
}
第三个servlet
package com.diy.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* here is an original servlet class
*/
@WebServlet("/demo3")
public class ServletDemo3 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//According to different request ways, response different handling
HttpServletRequest HSR = (HttpServletRequest) servletRequest;//this arg offered by tomcat at signature
//you already have the request object, you can get the request way
String method = HSR.getMethod();
//judge it is get or post
if ("GET".equals(method)){
//doGet handling logic
}else if ("POST".equals(method)){
//doPost handling logic
}
/**
* 一个只??Servlet接口的Servlet?的service?次都要??上面的代?区??理get和post?求方式
* 所以最后就写一个父??所有的servlet???提供??个方法,那就是HttpServlet
* 此??制本ServletDemo3,新建一个diy的Servlet父?
*/
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
自建servlet父类
package com.diy.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* here is an original servlet class
*/
public class myHttpServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
//According to different request ways, response different handling
HttpServletRequest HSR = (HttpServletRequest) req;//this arg offered by tomcat at signature
//you already have the request object, you can get the request way
String method = HSR.getMethod();
//judge it is get or post
if ("GET".equals(method)){
doGet(req,resp);
}else if ("POST".equals(method)){
doPost(req,resp);
}
}
/**
* 提供??承?的doGet和doPost方法,用protected修?
* @param req
* @param resp
*/
protected void doPost(ServletRequest req, ServletResponse resp) {
}
protected void doGet(ServletRequest req, ServletResponse resp) {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
第四个servlet
package com.diy.web;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/demo4")
public class ServletDemo4 extends myHttpServlet{
@Override
protected void doGet(ServletRequest req, ServletResponse resp) {
System.out.println("doGet Method ~~~");
}
@Override
protected void doPost(ServletRequest req, ServletResponse resp) {
System.out.println("doPost Method ~~~");
}
}
第五个servlet
package com.diy.web;
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;
/**
* urlPattern:一个servlet可以配置多个visit路径,多个visit路径是一个array
*/
@WebServlet(urlPatterns = {"/demo5a", "/demo5b"})
public class ServletDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo5 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo5 post ~~~");
}
}
第六个,以下四个是测试servlet的访问路径不同匹配范围的写法,匹配优先度,按从小到大的范围
package com.diy.web;
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;
/**
* urlPattern:匹配 FanWei
* 1.精que匹配
*/
@WebServlet(urlPatterns = "/user/select")
public class ServletDemo6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo6 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo6 post ~~~");
}
}
第七个
package com.diy.web;
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;
/**
* urlPattern:匹配 FanWei
* 2.目lu匹配
*/
@WebServlet(urlPatterns = "/user/*")
public class ServletDemo7 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo7 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
第八个
package com.diy.web;
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;
/**
* urlPattern:匹配 FanWei
* 3.kuo展名匹配
*/
@WebServlet(urlPatterns = "*.ext")//kuo展名匹配不要在此写斜杠
public class ServletDemo8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo8 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
第九个,匹配的最大范围,任意匹配
package com.diy.web;
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;
/**
* urlPattern:匹配rule
* 4.任意匹配
* 任意匹配的servlet用 / 之后会覆盖掉tomcat的default servlet,default servlet是用作 visit static zi源的,比如本例中的test.html
* 原本?可以在xiang目visit路径之后加zi源名, test.html,就能visit到,但当自写的servlet用@WebServlet(urlPatterns = "/")之后
* 默?的servlet就会被覆盖,就无法再加xiang目路径加?源名test.html visit本page面了
*/
//@WebServlet(urlPatterns = "/")
//或者/*,但/*you先ji高于chun/ , 因任意匹配会覆盖默?servlet,所以一般不配
public class ServletDemo9 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo9 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
第十个,试用xml代替注解配置访问路径
package com.diy.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 本demo用xml方式,而不用注解配置,所yi配置就是配visit路径,注解配置是servlet3.0之后?始支持
*/
public class ServletDemo10 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo10 get ~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// System.out.println("demo10 post");
}
}
第十个的路劲在web.xml配置文件中的写法
<?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_4_0.xsd"
version="4.0">
<!--1.配全类名-->
<servlet>
<servlet-name>demo10</servlet-name>
<servlet-class>com.diy.web.ServletDemo10</servlet-class>
</servlet>
<!--2.配访问路径-->
<servlet-mapping>
<servlet-name>demo10</servlet-name>
<url-pattern>/demo10</url-pattern>
</servlet-mapping>
</web-app>