zoukankan      html  css  js  c++  java
  • Servlet的生命周期

     Servlet的生命周期

       Servlet的生命周期是由tomcat服务器来控制的。

      1 构造方法:

    创建servlet对象的时候调用。默认情况下,第一访问servlet就会创建servlet对象只创建一次。说明servlet对象在tomcat中是单实例的。

     2初始化   init方法

      当创建完servlet对象的时候会调用init()方法,只调用一次。

          

       1>ServletConfig对象

           获取serlvet初始化参数

             调用的时候用

            this.getInitParameter(参数名)

       2>ServletContext对象  

           获取上下文信息

             A 获取全局参数

               ServletContext  sc= this.getServletContext()

               sc.getInitParameter("ecode");       

               sc.getContextPath()   项目路径  --项目名字

               sc.getRealPath()      发布路径  --发布后在tomcat中的路径  

    3 调用服务 service 方法   其中就包含doGet  doPost等方法

      每次发送请求的时候调用。可以调用n次。

    4 销毁    destory 方法

       销毁servlet对象的时候调用,停止服务器或者重新部署的web项目的时候销毁servlet就会调用destory方法

    package com.bw.reg.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.bw.reg.bean.Users;
    import com.bw.reg.service.ServiceImpl;
    
    public class LoginServlet extends HttpServlet {
    
    	/**
    	 * Constructor of the object.
    	 */
    	public LoginServlet() {
    		super();
    		System.out.println("这里是构造方法");
    	}
    
    	/**
    	 * Destruction of the servlet. <br>
    	 */
    	public void destroy() {
    		super.destroy(); // Just puts "destroy" string in log
    		System.out.println("销毁");
    		// Put your code here
    	}
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out
    				.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    		out.println("  <BODY>");
    		out.print("    This is ");
    		out.print(this.getClass());
    		out.println(", using the GET method");
    		out.println("  </BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    
    	/**
    	 * The doPost method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to post.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 * 
    	 */
    	
    	//这里是service方法
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
            
    		boolean isLogin = false;
    		
    		//设置编码类型
    		request.setCharacterEncoding("UTF-8");
    		
    		//获取登入时输入的用户名和密码
    		String username = request.getParameter("username");
    		String password = request.getParameter("pwd");
    		
    		//创建users的对象
    		Users users = new Users();
    		//给users的对象设置登入时输入的用户名和密码
    		users.setUserName(username);
    		users.setPwd(password);
    		//登入时的查找
    		ServiceImpl serImpl = new ServiceImpl();
    		isLogin = serImpl.find(users);
    	
    		if(isLogin){
    			System.out.println("登入成功!!");
    		}else {
    			System.out.println("登录失败");
    		}
    	}
    
    	/**
    	 * Initialization of the servlet. <br>
    	 *
    	 * @throws ServletException if an error occurs
    	 */
    	public void init() throws ServletException {
    		// Put your code here
    		System.out.println("这里是初始化方法");
    	}
    
    }
    
  • 相关阅读:
    3--Selenium环境准备--Eclipse 引入 selenium-server包
    2--Selenium环境准备--第一次使用Testng
    1--Selenium环境准备--Eclipse 添加Testng插件
    2--Jmeter 4.0--Excel 数据驱动 接口测试
    1--Jmeter4.0连接Oracle数据库
    冲刺第六天
    构建执法阅读笔记5
    学习进度八
    冲刺第五天
    冲刺第四天
  • 原文地址:https://www.cnblogs.com/houjiie/p/6212709.html
Copyright © 2011-2022 走看看