1

I have a Java file which has a useful code and I want to call that Java code in my JSP file. I have tried this, for instance I am using a Java file which successfully sends email to a mail ID. But if I call it in a JSP page its running error free but the email is not sent.

Java code:

package com.me;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SSL {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication("prakash.d2222","**********");
            }
          });

        try {     
          Message message = new MimeMessage(session);
          message.setFrom(new InternetAddress("from@no-spam.com"));
          message.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("prakash_d22@rediffmail.com"));
          message.setSubject("Testing Subject");
          message.setText("Dear Mail Crawler," +
                "from core java");
          Transport.send(message);

          System.out.println("Doneit");

        } catch (MessagingException e) {
          throw new RuntimeException(e);
        }
    }
}

and my JSP code is:

<html>
<body>
  <jsp:useBean id="link" scope="application" class = "com.me.SSL" />            
  <% out.println("ok"); %>    
</body>
</html>

and tom cat folder config is

webapps\root\web-inf
                   |
                   -classes\com\me\SSL.class
                   |                   
                   -lib\mail.jar
Roman C
  • 49,761
  • 33
  • 66
  • 176
prakash_d22
  • 1,153
  • 5
  • 21
  • 34

2 Answers2

1

you should try calling the method in the SSL class. From your code , it seems you are just creating jsp:usebean to get an instance of the object .

Try ${link.MethodName}

Metalhead
  • 1,429
  • 3
  • 15
  • 34
  • but i am using main ().how can it be called ? – prakash_d22 Aug 18 '12 at 10:56
  • Check point 2 in this link http://stackoverflow.com/questions/6395621/how-to-call-a-static-method-in-jsp-el .It explains how you can create a taglib function and use it in your jsp or You can call the main method just like calling any other method from java code but using scriplets . <% SSL x = new SSL(); x.main(null); %> . Passing null is fine unless you are passing any arguments to main method. – Metalhead Aug 18 '12 at 11:15
1

You can use

<jsp:useBean id="link" scope="application" class = "com.me.SSL" />          
<jsp:setProperty name="link" property="prop" value=""/>
<% out.println("ok"); %>    

and modify code

public class SSL {
  String prop;

  public String getProp() {
    return prop;
  }

  public void setProp(String prop) {
    this.prop = prop;
    main(null);
  }

  public static void main(String[] args) {
    Properties props = new Properties();

this means that you call main method when property is set.

if you were not using jsp:useBean

<%@ page import="com.me.SSL" %>
<html>
<body>
  <% new SSL().main(null); out.println("ok"); %>    
</body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176