简体   繁体   English

在jsp中调用java文件

[英]calling java file in jsp

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 我正在创建一个wepage,我想使用gmail smtp发送邮件。我尝试使用jsp页面,但是我没有工作,所以我在核心Java中创建了一个代码,该代码成功发送了email.now我想在我的Java中使用此Java代码JSP页面。我试过我,但出现错误

java code: Java代码:

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 我的jsp代码

<%@ 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. 所以请帮助我,因为我是jsp的新手。

Consider following suggestions: 考虑以下建议:

  1. .class files must be placed under WEB-INF/classes .class文件必须放在WEB-INF / classes下
  2. Never create a class in default package. 切勿在默认程序包中创建

package com.me;

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

And I can't see any getter/setter method in your class so no need to use <jsp:setProperty/> action in jsp page and don't include extension to class attribute. 而且我在您的类中看不到任何getter/setter方法,因此无需在jsp页面中使用<jsp:setProperty/>操作,并且不包括对class属性的扩展。

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

EDIT: 编辑:

If you're NOT using IDE (netbeans/eclipse) then you must have to create folder structure under /tomcat xx/webapps/ . 如果您使用IDE(netbeans / eclipse),则必须在/tomcat xx/webapps/下创建文件夹结构。

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

You have to call show() method in JSP page: 您必须在JSP页面中调用show()方法:

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

OR 要么

<%
   com.me.SSL obj=new com.me.SSL();
   obj.show();
%>

Firslt modify your class SSL 首先修改您的类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);
        }
    }
}

Just make a custom bean class that will work with you class SSL and name it like SSLImpelmenter 只需创建一个与您的SSL类一起使用的自定义bean类,并将其命名为SSLImpelmenter

where your class is 你上课的地方

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

   //getter setter methods for objSSL
}

Now in your JSP 现在在您的JSP中

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

SSL objSSLJSP = link.getObjSSL(); 
objSSLJSP.SendMail();

The immediate problem is that you are using a filename instead of a class name in the "class=..." attribute. 直接的问题是您在“ class = ...”属性中使用文件名而不是类名。 Since your class is declared in the default package (because you don't have a package declaration in your class), you should write the useBean as: 由于您的类是在默认包中声明的(因为您的类中没有package声明),因此您应该将useBean编写为:

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

And if you haven't done so already, you should make sure that the "SSL.class" file is on the classpath for your webapp. 并且,如果尚未这样做,则应确保“ SSL.class”文件位于Web应用程序的类路径中。 For instance, it could be in the "/WEB-INF/classes/" directory, or it could be in a JAR file in "/WEB-INF/lib/". 例如,它可以在“ / WEB-INF / classes /”目录中,也可以在“ / WEB-INF / lib /”中的JAR文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM