标签:
public void init() throws ServletException { // Put your code here System.out.println("我们的线程启动了"); SendEmail send=new SendEmail(); send.sendmail(); }
package com.lyw.servlet; public class SendEmail extends Thread { public void sendmail(){ int i=0; while(true){ i++; try { Thread.sleep(10*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("发送第"+i+"份邮件"); } } }
<load-on-startup>2</load-on-startup>
我们的<load-on-startup>标签中夹的数字是什么意思???有什么作用??
这代表我们启动是顺序,因为在实际开发中我们肯定要初始化多个对象,所以我们在这里要用数字表明启动的顺序。
<init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param>
package com.lyw.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; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String encoding=this.getServletConfig().getInitParameter("encoding"); response.setCharacterEncoding(encoding); PrintWriter out=response.getWriter(); out.print("编码是:"+encoding); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here System.out.println("我们的线程启动了"); SendEmail send=new SendEmail(); send.sendmail(); } }
读取配置信息,该配置信息只能由该Servlet读取。但是,如果希望让所有的Servlet读取某个参数,应该这样配置:<contex-param>标签下配置的可以被任何Servlet读取。
标签:
原文地址:http://www.cnblogs.com/huiyuantang/p/5351102.html