欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet的三种创建

程序员文章站 2022-05-18 15:30:57
...

servlet有三种手动创建的方式:

第一种:实现Servlet接口

了解一下Servlet生命周期

A、在容器中创建一个Servlet对象 (放到容器中,如果容器中存在,不创建)

B、初使化Servlet(调用init方法)

C、调用service方法,服务用户的请求;

D、调用destroyed方法,销毁对象;

package cn.lanqiao.servlet;

import java.io.IOException;
import java.util.Enumeration;

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

public class Servlet6 implements Servlet {

	private ServletConfig config;

	// 1.创建servlet对象
	public Servlet6() {
		System.out.println("1 servlet对象创建。。。");
	}

	// 2.初始化servlet
	@Override
	public void init(ServletConfig conf) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("2,servlet初始化 了。。。。。。");
		this.config = conf;
	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// System.out.println(res);
		// res.getWriter().write("调用service方法。。。");
		res.getOutputStream().write("hello".getBytes());
		System.out.println("3 service方法被调用,响应用户请求");
		// 通过servletconfig拿初始化参数
		// String name = config.getInitParameter("uname");
		// String upassword = config.getInitParameter("upassword");
		// System.out.println(name);
		// System.out.println(upassword);
		Enumeration<String> names = config.getInitParameterNames();
		while (names.hasMoreElements()) {
			String str = (String) names.nextElement();
			System.out.println(str + "---" + config.getInitParameter(str));

		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("4 销毁对象。。。。。");
	}

}

第二种:继承GenericServlet,实现里面的Service方法

package cn.lanqiao.servlet;

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 Servlet2 extends GenericServlet {

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = res.getWriter();
		pw.write("hello,servlet2,,,,,,,");
	}

}

第三种:继承HttpServlet,实现里面的doGet方法和doPost方法,一般我们都是用这个创建Servlet对象

package cn.lanqiao.servlet;

import java.io.IOException;

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

public class Servlet5 extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("hello,servlet5...");
	}
}

以上三种手动创建Servlet对象的方法在发布的时候需要去配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>servletproject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Servlet1</servlet-name>
    <servlet-class>cn.lanqiao.servlet.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet1</servlet-name>
    <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>
  
  
</web-app>

但是我们使用直接创建一个Servlet的时候,会自动帮我们在web.xml中配置,对于新手还是使用第三种手动创建对象的方法好,这种直接创建的方法在以后的工作中使用可以提高效率。