servlet的细节继续

Posted helloworld2019

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了servlet的细节继续相关的知识,希望对你有一定的参考价值。

1.需求:初始化任务,定时任务(定时发送邮件)

解决方法:通过<load-om-startup>配合线程知识搞定

在配置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>test</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>ServletTest</servlet-name>
    <servlet-class>ServletTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletTest</servlet-name>
    <url-pattern>/ServletTest</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>MyInitServlet</servlet-name>
    <servlet-class>com.myservlet.MyInitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyInitServlet</servlet-name>
    <url-pattern>/MyInitServlet</url-pattern>
  </servlet-mapping>

</web-app>
View Code

技术图片

 可以实现定时发送邮件功能

MyInitServlet.java:

技术图片
package com.myservlet;

import java.io.IOException;
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 MyInitServlet
 */
@WebServlet("/MyInitServlet")
public class MyInitServlet extends HttpServlet 
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyInitServlet() 
        super();
        // TODO Auto-generated constructor stub
    

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        // TODO Auto-generated method stub
    

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        // TODO Auto-generated method stub
    
    public void init() throws ServletException
        System.out.println("init has been used");
        System.out.println("创建数据库,表,读取数据参数");
        //创建一个线程
        SendEailThred sendemail =new SendEailThred();
        sendemail.start();
    

View Code

Thread..java

技术图片
package com.myservlet;

public class SendEailThred extends Thread
@Override
public void run()
    int i=0;
    while(true)
    try 
        Thread.sleep(10*1000);
        System.out.println("We have send No."+(++i)+"  email");
catch (Exception e) 
        // TODO: handle exception
        e.printStackTrace();
    

View Code

实现的结果:

技术图片

<load-on-startup><load-om-startup>可以设置启动顺讯

 26:12分钟继续

以上是关于servlet的细节继续的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

Servlet细节

2016-2-1 Servlet细节

Servlet的细节

Servlet 进阶小细节

request的细节--学习笔记