目前使用servlet最常用的方法有三种,

       1、实现servlet,

       2、继承GenericServlet实现service方法,

       3、继承HttpServlet

       4、配置文件 , 以上每一步在web.xml配置之后才可以实现!

      完整项目如下:


        1、实现servlet,

  

package com.itcast;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyServlet implements Servlet{
	/**
	 * 销毁Servlet
	 */
	@Override
	public void destroy() {
		
	}
	/**
	 * 取得servlet的配置
	 */
	@Override
	public ServletConfig getServletConfig() {
		return null;
	}
	/**
	 * 取得servlet的一些信息
	 */
	@Override
	public String getServletInfo() {
		return null;
	}
	/**
	 * 对servlet进行初始化
	 */
	@Override
	public void init(ServletConfig arg0) throws ServletException {
		System.out.println("init it! ");
	}
	
	/**
	 * 最主要的业务处理
	 * ServletRequest  从browser获得信息
	 * ServletResponse 反馈给browser信息
	 */
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		System.out.println("service it!");
		PrintWriter pw = arg1.getWriter();
		pw.println("Hello Servlet!");
	}

}

          2、继承GenericServlet实现service方法,

package com.itcast;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyGenericServlet extends GenericServlet{

	private static final long serialVersionUID = 1L;

	//仅仅是重写一个service的方法就好了
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		PrintWriter pw = arg1.getWriter();
		pw.println("Hi GenericServlet!!");
	}
}
       3、继承HttpServlet

package com.itcast;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyHttpServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	//处理get请求
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{
		PrintWriter pw = res.getWriter();
		pw.println("Sayhi to HttpServlet!!");
	}
	public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException{
		this.doGet(req, res);
	}
}

      4、配置文件 , 以上每一步在web.xml配置之后才可以实现!

           

       如上:资源ONE对应servlet-mapping 的 url-pattern ,然后就可以找到对应的serlvet-name,通过相同的servlet-name去找servlet-class(servlet的类)

      这个过程就是 url资源找到相对应的资源(程序)

      完整的配置文件如下:

<?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"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
   <!-- 1、实现servlet -->
  <servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>com.itcast.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/ONE</url-pattern>
  </servlet-mapping>
  
   <!-- 2、继承GenericServlet -->
  <servlet>
    <servlet-name>GenericHttp</servlet-name>
    <servlet-class>com.itcast.MyGenericServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>GenericHttp</servlet-name>
    <url-pattern>/TWO</url-pattern>
  </servlet-mapping>

  <!-- 3、继承HttpServlet -->
  <servlet>
    <servlet-name>HttpServlet</servlet-name>
    <servlet-class>com.itcast.MyHttpServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HttpServlet</servlet-name>
    <url-pattern>/Three</url-pattern>
  </servlet-mapping>

</web-app>



Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐