如何使用按钮从jsp页面执行java代码?

时间:2015-03-10 18:48:05

标签: javascript jsp java-ee jsp-tags

这是我的jsp代码:

EMAIL.JSP

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Example</title>
  </head>
  <body>
    <form method="post" action="">
        <center>
        <table border="1" width="30%" cellpadding="3">
            <thead>
                <tr>
                    <th colspan="2">Enter the information</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>To Address</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
                <tr>
                    <td>Subject</td>
                    <td><input type="text" name="uname" value="" /></td>
                </tr>
               <tr>

                    <td>AttachFile</td>
<td<inputtype="text"value=".\ogc\hb1_800.jpg"id=".\ogc\hb1_800.jpg" />
                  </td>
                </tr>
                <tr>
                    <td><input type="reset" value="Clear" /></td>
                    <td><input type="Submit" value="Submit" /></td> 
                </tr>  
            </tbody>
        </table>
        </center>
    </form> 

点击提交按钮我需要执行java代码:          final.java如下: -

FINAL.JAVA

   package com.grid;


   public class final {  

         public static void main(String[] args) {  

            String ToAddress = new String[]{"8789951@gmail.com"};  
            String Subject = "Hi this is test Mail";   
            String AttachFile = {".\ogc\notepad\styles\thumbs\hb1_800.jpg"};  
            new Email().sendMail(ToAddress,Subject,AttachFile);   

       }  
   }  

它调用email.java

   package com.grid;  


   import java.io.*;
   import java.util.*;
   import java.util.logging.Level;
   import java.util.logging.Logger;

   import javax.mail.Authenticator;
   import javax.mail.Message;
   import javax.mail.MessagingException;
   import javax.mail.Multipart;
   import javax.mail.PasswordAuthentication;
   import javax.mail.Session;
   import javax.mail.Transport;
   import javax.mail.internet.InternetAddress;
   import javax.mail.internet.MimeBodyPart;
   import javax.mail.internet.MimeMessage;
   import javax.mail.internet.MimeMultipart;

   public class Email{  


       private String SMTP_HOST = "smtp.gmail.com";  
       private String FROM_ADDRESS = "n123f@gmail.com";  
       private String PASSWORD = *****";  
       private String FROM_NAME = "Abc";  

       public boolean sendMail(String ToAddress,String Subject,String AttachFile) {  
           try {  
               Properties props = new Properties();  
               props.put("mail.smtp.host", SMTP_HOST);  
               props.put("mail.smtp.auth", "true");  
               props.put("mail.debug.auth", "true");  
               props.put("mail.smtp.ssl.enable", "true");

               Session session = Session.getInstance(props, new SocialAuth());  
               MimeMessage msg = new MimeMessage(session);

               InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);  
               msg.setFrom(from);  

               InternetAddress[] ToAddress = new   InternetAddress[recipients.length];  
               for (int i = 0; i < recipients.length; i++) {  
                   ToAddress = new InternetAddress(recipients[i]);  
               }  
               msg.setRecipients(Message.RecipientType.TO, ToAddress);  

               msg.setSubject(Subject);
               MimeBodyPart msgBodyPart = new MimeBodyPart();

              // Fill the message
              msgBodyPart.setText("This is message body");

              // Create a multipart message
              Multipart multipart = new MimeMultipart();

              // Set text message part
              multipart.addBodyPart(msgBodyPart);

              // Part two is attachment 
              if (FileName != null && FileName.length > 0) 
              {
                 for (String filePath : FileName) 
                 {
                     MimeBodyPart attachPart = new MimeBodyPart();

                     try {
                         attachPart.AttachFile(filePath);
                     } catch (IOException ex) {
                         ex.printStackTrace();
                     }

                     multipart.addBodyPart(attachPart);
                 }
              }

             /*DataSource source = new FileDataSource(filename);
             msgBodyPart.setDataHandler(new DataHandler(source));
             msgBodyPart.setFileName(filename);
             multipart.addBodyPart(msgBodyPart);*/

             msg.setContent(multipart);  
             Transport.send(msg);  
             return true;  
         } catch (UnsupportedEncodingException ex) {  
             Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null,  ex);  
             return false;  
        } catch (MessagingException ex) {  
             Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null,ex);  
             return false;  
        }  
    }  


    class SocialAuth extends Authenticator {  

        @Override  
        protected PasswordAuthentication getPasswordAuthentication() {  

            new PasswordAuthentication(FROM_ADDRESS, PASSWORD);  

        }
    }
}

现在我怀疑如何在单击email.jsp页面中的按钮提交时执行final.java(这是一个正在运行的代码)?

1 个答案:

答案 0 :(得分:0)

这是一个用于演示的简单servlet:

JSP:

<form method="post" action="/email">
<center>
<table border="1" width="30%" cellpadding="3">
 <thead>
    <tr>
        <th colspan="2">Enter the information</th>
    </tr>
    </thead>
<tbody>
    <tr>
        <td>To Address</td>
        <td><input type="text" name="to" value="" /></td>
    </tr>
    <tr>
        <td>Subject</td>
        <td><input type="text" name="subject" value="" /></td>
        </tr>
    <tr>
        <td>AttachFile</td>
        <td> <input type="text" name="file" value=".\ogc\hb1_800.jpg"id=".\ogc\hb1_800.jpg" />
        </td>
    </tr>
    <tr>
        <td><input type="reset" value="Clear" /></td>
        <td><input type="Submit" value="Submit" /></td> 
    </tr>  
</tbody>
 </table>
</center>
</form> 

的Servlet

@WebServlet(name="mytest", urlPatterns={"/email"})
public class MailServlet extends javax.servlet.http.HttpServlet{
   public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    String to = request.getParameter("to");
    String subject = request.getParameter("subject");
    String file = request.getParameter("file");

    new Email().sendMail(to,subject,file);

    response.getWriter().println("Email sent");
}

}