1

我需要创建一个小文件实用程序类以从 Windows 桌面运行命令行。代码已经完成,但是,在查看了如何打包它之后,它需要来自主应用程序的自定义框架才能运行。不要问为什么,因为这需要一段时间才能回答,只要把它当作一个有效的假设。

无论如何,现在他们想要一个 jsp 来调用这个类,但他们仍然希望它更像是一个单独的实用程序,即使它是主代码库的一部分。他们还希望它调用实用程序中的 main 方法,这对我来说听起来不是很好的设计,但他们不想将其更改为 servlet 类。

该程序只接受一些参数,然后基本上对文件进行一步操作,然后完成。不是真正的 jsp 类型请求/响应方案,但我不是编写需求的人。

从设计的角度来看,对于简单的实用程序应用程序是否有更好的方法来做到这一点?

谢谢,

詹姆士

4

3 回答 3

3

如果您真的无法更改设计,您可以只导入该类并调用它(仅当它位于 webapp 的类路径中时才有效)。

YourMainClass.main(new String[] {"some", "arguments"});

或者生成一个进程并执行它(这真的不推荐,因为新进程会分配另一堆内存,这与服务器当前使用的内存一样多!)。

Runtime.getRuntime().exec("java -cp /path/to/root com.example.YourMainClass some arguments");

这两种方式都可以在scriptlet (yuck) 中完成,或者最好只在 Servlet 中完成。

于 2011-09-22T15:42:04.587 回答
2

让该main方法调用服务/库方法。JSP 可以调用相同的服务/库方法。无需显式调用main; 从一开始就让它干净。

于 2011-09-22T15:42:57.693 回答
0

T**这是我的 JavaMail.java** 它是包含 Main 方法的 java 类...

Public Class JavaMAil{
String d_email = "abc@gmail.com",//you email address
        d_password = "XXXX", //your email password
        d_host = "smtp.gmail.com",
        d_port = "465",
        m_to = "xyz@gmail.com    ", // Target email address
        m_subject = "Testing",
        m_text = "Hey, this is a test email.";



public JavaMail() {
    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    //props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    try {
        SMTPAuthenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);     
        MimeMessage msg = new MimeMessage(session);
        msg.setText(m_text);
        msg.setSubject(m_subject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
        Transport.send(msg);
    } catch (Exception mex) {
        mex.printStackTrace();
    }
}

public static void main(String[] args) {
    JavaMail blah = new JavaMail();
}

private class SMTPAuthenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(d_email, d_password);
    }
}

}

**This is my mail.jsp**

And It is jsp it Invokes Main method from the class called JavaMail




<%@ page import="mail.JavaMail" %>
<%
JavaMail obj = new JavaMail();
%>
于 2014-05-26T07:19:30.727 回答