Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将jsp表单提交到servlet会生成空白输出_Java_Jsp_Servlets - Fatal编程技术网

Java 将jsp表单提交到servlet会生成空白输出

Java 将jsp表单提交到servlet会生成空白输出,java,jsp,servlets,Java,Jsp,Servlets,我试图从包含文本框和提交按钮的jsp表单发送邮件。当用户在文本框中输入电子邮件id并单击submit按钮时,将调用servlet页面。在这里,url被更改为servlet url,它生成空白页面,邮件将无法发送 这是我的demo1.jsp 我已经导入了mail.jar、activation.jar库。 现在请告诉我哪里出错了? 请帮助我通过它发送邮件。决定是要在servlet中写入响应,还是重定向到jsp……这两种方法都做不到 也就是说,仅当发送邮件失败时,才设置邮件头并将其写入 protec

我试图从包含文本框和提交按钮的jsp表单发送邮件。当用户在文本框中输入电子邮件id并单击submit按钮时,将调用servlet页面。在这里,url被更改为servlet url,它生成空白页面,邮件将无法发送

这是我的demo1.jsp

我已经导入了mail.jar、activation.jar库。 现在请告诉我哪里出错了?
请帮助我通过它发送邮件。

决定是要在servlet中写入响应,还是重定向到jsp……这两种方法都做不到

也就是说,仅当发送邮件失败时,才设置邮件头并将其写入

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, Exception
{
    try
    {
        mail.sendMail(request.getParameter("txtmail"));
        response.sendRedirect("demo1.jsp");
    }
    catch(Exception e)
    {   
        try 
        {
          PrintWriter out = response.getWriter();
          response.setContentType("text/html;charset=UTF-8");
           out.println(request.getParameter("txtmail"));
           out.println("error in mail"+e);
        }
        catch(Exception ew) { /* error writing to out */ }
    }
}

使用gmail服务器尝试此代码

公共静态void sendMailString uEmail引发异常{ 属性=新属性

    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "587");

    Session session = Session.getDefaultInstance(properties,  
               new javax.mail.Authenticator() {  
               protected PasswordAuthentication getPasswordAuthentication() {  
               return new PasswordAuthentication("yourEmail","yourpassword");//change accordingly  
               }  
              });

    MimeMessage message=new MimeMessage(session);
    try {
        InternetAddress headers=new InternetAddress();
        message.setFrom(new InternetAddress("youremail"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(uEmail));
        message.setSubject("Welcome \n your email:"+uEmail);


        message.setText(html, "UTF-8", "html");

        Transport.send(message);
System.out.println("msg success ");
    } catch (AddressException e) {

        e.printStackTrace();
    } catch (MessagingException e) {

        e.printStackTrace();
    }


}

事实上,我尝试了这两种方法中的任何一种,只是忘记了删除或注释一个代码。我主要关注的是发送邮件,无论它是否重定向或显示一些消息。@kunalkakkad,我看到您尝试了两种方法,但一旦您开始编写响应和设置标题等,您就无法重定向。请参阅我对我的答案的更新。好吧,不管你是否改变代码,解决我的问题?@ Kunalkkad,它可能无法解决发送邮件的问题,但是你不应该再获得空白页面。@ Kunalkkad,你编译,重新启动应用程序还是Tomcat服务器?也许你还需要清除缓存。使用Gmail服务器意味着使用Gmail帐户?是的,YouReMAil和password>return new password Authentication yourEmail,yourpassword;//在此也相应更改message.setFromnew InternetAddress yourEmail;我必须在三个位置更改,对吗?1您的电子邮件2您的密码3您的电子邮件
And sendmail method is defined in <b>mail.java</b>
<code>
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author KuNaL-IT
 */
public class mail {

    public static void sendMail(String e_mail) throws Exception {
        String sender = "kunal.k.kakkad@gmail.com";
        String pass = "abcdefghijkl";
        String rec = e_mail;
        String msg = "Hello This is sample test mail...";
        String sub = "Test Subject";
        // System.out.println(rec);
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", "smtp.gmail.com");
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");
        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();
        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(sub);
        message.setContent(msg, "text/plain");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(rec));
        transport.connect("smtp.gmail.com", 465, sender, pass);
        transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
        System.out.println(" message send");
        transport.close();
    }

}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, Exception
{
    try
    {
        mail.sendMail(request.getParameter("txtmail"));
        response.sendRedirect("demo1.jsp");
    }
    catch(Exception e)
    {   
        try 
        {
          PrintWriter out = response.getWriter();
          response.setContentType("text/html;charset=UTF-8");
           out.println(request.getParameter("txtmail"));
           out.println("error in mail"+e);
        }
        catch(Exception ew) { /* error writing to out */ }
    }
}
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "587");

    Session session = Session.getDefaultInstance(properties,  
               new javax.mail.Authenticator() {  
               protected PasswordAuthentication getPasswordAuthentication() {  
               return new PasswordAuthentication("yourEmail","yourpassword");//change accordingly  
               }  
              });

    MimeMessage message=new MimeMessage(session);
    try {
        InternetAddress headers=new InternetAddress();
        message.setFrom(new InternetAddress("youremail"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(uEmail));
        message.setSubject("Welcome \n your email:"+uEmail);


        message.setText(html, "UTF-8", "html");

        Transport.send(message);
System.out.println("msg success ");
    } catch (AddressException e) {

        e.printStackTrace();
    } catch (MessagingException e) {

        e.printStackTrace();
    }


}