zoukankan      html  css  js  c++  java
  • 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>可以设置启动顺讯

    下面的utf-8将会被写死

    重点说明;全局配置

    实现结果;

    MyServlet1.java

    package com.hello;
    
    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 MyServlet1
     */
    //@WebServlet("/MyServlet1")
    public class MyServlet1 extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public MyServlet1() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            
            
            String encoding=this.getServletConfig().getInitParameter("encoding");
    //        response.setCharacterEncoding(this.getServletConfig().getInitParameter("encoding"));
            response.setCharacterEncoding(encoding);
            PrintWriter out=response.getWriter();
            out.print("编码是"+encoding);
    //         response.getWriter().println("hello,world!"+new java.util.Date().toString());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);    
        }
    
    }
    View Code

    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>MyServlet1</servlet-name>
        <servlet-class>com.hello.MyServlet1</servlet-class>
        <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>MyServlet1</servlet-name>
        <url-pattern>/MyServlet1</url-pattern>
      </servlet-mapping>
    </web-app>
    View Code

     可以使用枚举遍历;

  • 相关阅读:
    康师傅JVM:运行时数据区概述及线程(三)
    康师傅JVM:程序计数器(四)
    Git常用命令
    Arthas概述
    康师傅JVM:JVM与Java体系结构(一)
    LabVIEW 连接MySQL数据库
    LabVIEW dll 崩溃
    LabVIEW 关于定时的研究
    NI 配置管理软件MAX的一些功能使用介绍
    LabVIEW 串口通信
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10990533.html
Copyright © 2011-2022 走看看