我有一个Java文件,它有一个有用的代码,我想在我的JSP文件中调用这个Java代码。例如,我使用了一个Java文件,该文件成功地将电子邮件发送到邮件ID,但如果我在JSP页面中调用它,它的运行错误是自由的,但电子邮件不会发送。
Java代码:
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);
}
}
}和我的JSP代码是:
<html>
<body>
<jsp:useBean id="link" scope="application" class = "com.me.SSL" />
<% out.println("ok"); %>
</body>
</html>而tom文件夹配置是
webapps\root\web-inf
|
-classes\com\me\SSL.class
|
-lib\mail.jar发布于 2012-08-18 11:05:38
您可以使用
<jsp:useBean id="link" scope="application" class = "com.me.SSL" />
<jsp:setProperty name="link" property="prop" value=""/>
<% out.println("ok"); %> 修改代码
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();这意味着在设置属性时调用main方法。
如果你不使用jsp:useBean
<%@ page import="com.me.SSL" %>
<html>
<body>
<% new SSL().main(null); out.println("ok"); %>
</body>
</html>发布于 2012-08-18 10:51:46
您应该尝试在SSL类中调用该方法。从您的代码中,您似乎只是创建jsp:usebean来获取对象的一个实例。
尝试${link.MethodName}
https://stackoverflow.com/questions/12017739
复制相似问题