0

i am creating a wepage where i want to send a mail using gmail smtp.i tried with jsp page but i did not work so ihave craeted a code in core java which is successfully sending email.now i want to use this java code in my jsp page .i tried i but got errors

java code:

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","password");
                }
            });

        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("hi");
            message.setText("12345" +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

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

my jsp code

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <html>
                <body>
                <jsp:useBean id="link" scope="application" class = "SSL.class" />
<jsp:setProperty name="link" property="*" />

                </body>
                </html>

and error shown is

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /pizza/page/ssl.jsp(7,4) The value for the useBean class attribute SSL.class is invalid.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1233)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1178)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
    org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
    org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
    org.apache.jasper.compiler.Generator.generate(Generator.java:3459)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:231)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717

so please help me as i am new to jsp.

4

3 回答 3

3

考虑以下建议:

  1. .class文件必须放在 WEB-INF/classes 下
  2. 永远不要在默认包中创建类。

package com.me;

public class SSL { 
    public void show(){
         ///
    }
}

而且我在您的类中看不到任何getter/setter方法,因此无需<jsp:setProperty/>在 jsp 页面中使用操作并且不包含class属性扩展。

<jsp:useBean id="link" scope="application" class="com.me.SSL" />

编辑:

如果您使用 IDE (netbeans/eclipse),那么您必须/tomcat x.x/webapps/.

/webapp   <--- This is known as `context` folder
|
|-------/WEB-INF
|       |
|       |-----------/classes
|       |                  |---/com/me/SSL.class
|       | 
|       |-----------/lib
|                    mail.jar
| sample.jsp

您必须在 JSP 页面中调用 show() 方法:

<jsp:useBean id="link" scope="application" class="com.me.SSL" />
<%
  link.show();
%> 

或者

<%
   com.me.SSL obj=new com.me.SSL();
   obj.show();
%>
于 2012-08-18T06:51:27.920 回答
1

首先修改你的类SSL

public class SSL {

 public  void SendMail(){
        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","password");
                }
            });

        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("hi");
            message.setText("12345" +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

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

只需创建一个自定义 bean 类,该类将与您一起使用SSL类并将其命名为SSLImpelmenter

你的班级在哪里

public class SSLImpelmenter
{
   private SSL objSSL = new SSL();

   //getter setter methods for objSSL
}

现在在你的 JSP 中

//Add import for SSL Class
//Now Use useBean tag
<jsp:useBean id="link" scope="application" class = "SSLImpelmenter" />

SSL objSSLJSP = link.getObjSSL(); 
objSSLJSP.SendMail();
于 2012-08-20T06:42:42.880 回答
0

直接的问题是您在“class=...”属性中使用的是文件名而不是类名。由于你的类是在默认包中声明的(因为你的类中没有package声明),你应该写成useBean

<jsp:useBean id="link" scope="application" class="SSL" />

如果您还没有这样做,您应该确保“SSL.class”文件位于您的 web 应用程序的类路径中。例如,它可能位于“/WEB-INF/classes/”目录中,也可能位于“/WEB-INF/lib/”中的 JAR 文件中。

于 2012-08-18T08:07:18.047 回答