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

Servlet的生命周期

程序员文章站 2022-03-11 21:58:48
...

Servlet的生命周期

实例化---->初始化init()----->服务service()----->doGet/doPost方法------>销毁destory()

注意:实例化初始化只执行一次

Servlet的生命周期

1. index.jsp

​
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="WServlet">fdgadhbgb</a>

<form action="WServlet" method="get">
  <input type="text" name="age">
  <input type="submit">
</form>

<form action="WServlet" method="post">
  <input type="text" name="age">
  <input type="submit">
</form>

</body>
</html>

​

2.WServlet.java

package com.servlet;

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

import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WServlet
 */
//@WebServlet("/WServlet")
public class WServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WServlet() {
        super();
        // TODO Auto-generated constructor stub
        System.out.println("===========实例化==========");
    }
    

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("===========service===========");
		super.service(req, resp);
	}


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		System.out.println("=========destroy==========");
	}


	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub		
		super.init();
		System.out.println("==========init============");
	}


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("========doGet========");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =response.getWriter();
		out.println("doGet方法");
		String age =request.getParameter("age");
		out.println(age);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("=======doPost========");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =response.getWriter();
		out.println("doPost方法");
		String age =request.getParameter("age");
		out.println(age);
	}

}

3. 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" id="WebApp_ID" version="3.1">
  <display-name>Servlet123</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>WServlet</servlet-name>
    <servlet-class>com.servlet.WServlet</servlet-class>    
  </servlet>
  <servlet-mapping>
    <servlet-name>WServlet</servlet-name>
    <url-pattern>/WServlet</url-pattern>
  </servlet-mapping>
</web-app>

4. 效果图

Servlet的生命周期

相关标签: servlet