码迷,mamicode.com
首页 > 其他好文 > 详细

servlet的细节继续

时间:2019-06-08 14:43:17      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:startup   启动   event   oge   image   view   http   取数据   数据库   

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的细节继续

标签:startup   启动   event   oge   image   view   http   取数据   数据库   

原文地址:https://www.cnblogs.com/helloworld2019/p/10990533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!