创建servlet的三种方式
Posted sarvin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建servlet的三种方式相关的知识,希望对你有一定的参考价值。
第一种方式,实现Servlet接口
1 package com.example.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.Servlet; 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 11 public class testServlet1 implements Servlet { 12 13 /** 14 * 销毁方法 15 */ 16 public void destroy() { 17 System.out.println("这是销毁方法!"); 18 19 } 20 21 /** 22 * 初始化方法 23 */ 24 public void init(ServletConfig arg0) throws ServletException { 25 System.out.println("这是初始化方法!"); 26 } 27 28 /** 29 * 服务方法 30 */ 31 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 32 33 System.out.println("这是服务方法!"); 34 35 } 36 37 38 //获取servlet配置信息 39 public ServletConfig getServletConfig() { 40 41 return null; 42 } 43 44 //获取servlet信息 45 public String getServletInfo() { 46 47 return null; 48 } 49 }
第二种方式:继承GenericServlet抽象类
GenericServlet抽象类中只有一个抽象方法service()方法
1 package com.example.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.GenericServlet; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 10 public class testServlet2 extends GenericServlet{ 11 12 @Override 13 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 14 15 System.out.println("这是服务方法!"); 16 } 17 18 }
第三种方式:继承HttpServlet抽象类重写doPut()和doPost()方法
1 package com.example.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 public class testServlet3 extends HttpServlet{ 10 11 @Override 12 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 13 14 System.out.println("这是get方法提交"); 15 16 } 17 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 System.out.println("这是post方法提交"); 22 } 23 24 }
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>
以上是关于创建servlet的三种方式的主要内容,如果未能解决你的问题,请参考以下文章