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(){
         ///
    }
}

また、クラスにメソッドが表示されないため、jsp ページでアクションを使用する必要がなく、属性への拡張機能を含めないでください。getter/setter<jsp:setProperty/>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);
        }
    }
}

クラスSSLで動作するカスタムBeanクラスを作成し、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 に答える