创建servlet的三种方式

第一种方式,实现Servlet接口

 package com.example.servlet;

 import java.io.IOException;

 import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class testServlet1 implements Servlet { /**
* 销毁方法
*/
public void destroy() {
System.out.println("这是销毁方法!"); } /**
* 初始化方法
*/
public void init(ServletConfig arg0) throws ServletException {
System.out.println("这是初始化方法!");
} /**
* 服务方法
*/
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { System.out.println("这是服务方法!"); } //获取servlet配置信息
public ServletConfig getServletConfig() { return null;
} //获取servlet信息
public String getServletInfo() { return null;
}
}

第二种方式:继承GenericServlet抽象类

GenericServlet抽象类中只有一个抽象方法service()方法

 package com.example.servlet;

 import java.io.IOException;

 import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class testServlet2 extends GenericServlet{ @Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("这是服务方法!");
} }

第三种方式:继承HttpServlet抽象类重写doGet()和doPost()方法

 package com.example.servlet;

 import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; public class testServlet3 extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("这是get方法提交"); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("这是post方法提交");
} }

web.xml配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!-- sevlet配置 -->
<!-- 1.servlet的定义:告诉容器如何来创建servlet -->
<servlet>
<servlet-name>test1</servlet-name>
<servlet-class>com.example.servlet.testServlet1</servlet-class>
</servlet> <!-- 2.配置servlet路径 servlet映射 -->
<!-- url-pattern 必须以/开头 访问:http://localhost:8080/servletPro/login -->
<servlet-mapping>
<servlet-name>test1</servlet-name>
<url-pattern>/login1</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>test2</servlet-name>
<servlet-class>com.example.servlet.testServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test2</servlet-name>
<url-pattern>/login2</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>test3</servlet-name>
<servlet-class>com.example.servlet.testServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test3</servlet-name>
<url-pattern>/login3</url-pattern>
</servlet-mapping>
</web-app>
上一篇:【HDU 3038】 How Many Answers Are Wrong (带权并查集)


下一篇:巧用ELK快速实现网站流量监控可视化