Send mail via AJAX or multi-threaded for page not stuck

Source: Internet
Author: User
Tags tomcat

It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
Because sending messages with attachments can take a lot of time, so if you simply send the message in the form of an access servlet, it will cause the client page to stay, which is not very good, so we generally use AJAX to send the page, to achieve a page without refreshing, or by creating another thread , the main thread returns to the client and lets the child thread send the message, which enables the page to return in advance.

Program code

Mailbythreadservletservlet.java

Package edu.servlet.mail;

Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Mailbythreadservlet extends HttpServlet {public
	void doget (HttpServletRequest request, HttpServletResponse response)
			throws Servletexception, IOException {
		    response.setcontenttype ("text/html; Charset=utf-8 ");
             Sendmailthread SMT = new Sendmailthread ();
             Thread thread = new Thread (SMT);
             Thread.Start ();
             Response.getwriter (). Print ("The message is being sent, please wait a moment ...");

	public void DoPost (HttpServletRequest request, httpservletresponse response)
			throws Servletexception, IOException {
           doget (request, response);
	}

}
Child thread Sendmailthread.java

Package edu.servlet.mail;
Import Java.io.FileInputStream;
Import Javax.mail.Message;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.mail.internet.MimeUtility;
Import Javax.naming.Context;

Import Javax.naming.InitialContext;  public class Sendmailthread implements runnable{@Override public void Run () {try{Context initctx = new
	           InitialContext (); 
	           Tomcat places the created environment container in the initialization container and needs to find the context Envctx = (context) initctx.lookup ("java:comp/env") Through "java:comp/env" (fixed);
	          
	           In the Environment parameter container through "Mail/dog" to find the Session object Session session = (session) envctx.lookup ("Mail/dog"); /* * Verify that Tomcat's ClassLoader (Envctx.lookup ("Mail/dog")) differs from the application's class loader (Session) * But I don't know. Tao Why is the same result, supposedly is not the same, anyway is not very important * are org.apache.catalina.loader.StandardClassLoader * */S Ystem.out.println (Envctx.looKup ("Mail/dog"). GetClass (). getClassLoader (). GetClass (). GetName ());
	           System.out.println (Session.class.getClassLoader (). GetClass (). GetName ());
		     Set debug mode to print execution process session.setdebug (true);
			  Create a Message object that represents the content of the messages. Message msg = new MimeMessage (session,new fileinputstream ("C:\\Documents and settings\\administrator\\ desktop \ \
			   
			  Sendmultipartmessagedemo3.eml "));
			  Transport Transport = Session.gettransport ();
			  Transport.connect ("smtp.163.com", "xh216319", "Xiaoxiao"); Transport.sendmessage (msg, Internetaddress.parse (Mimeutility.encodetext ("ITSTAR") + "<ITSTAR1965@SINA.COM>
			"+mimeutility.encodetext (" Xiao Hua ") +" <xh216319@163.com> ");
 }catch (Exception e) {}finally{}}}
Visit: Http://localhost:8080/JavaMail_02/servlet/MailByThreadServlet, you will be back soon




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.