将java应用程序转换为jsp/servlet

问题描述 投票:0回答:4

我有一个接受分段上传的java应用程序,我的问题是我想要一个HTML/JSP前端,而不是仅仅在服务器上工作。根据我提供的代码,实现此目的的最佳方法是什么。这对我来说有点困惑,因为我不确定如何将文件上传部分带入 html/jsp 页面。任何建议都会有所帮助。

非常感谢, 语气

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ProgressEvent;
import com.amazonaws.services.s3.model.ProgressListener;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;

public class S3TransferProgressSample {

private static AWSCredentials credentials;
private static TransferManager tx;
private static String bucketName;

private JProgressBar pb;
private JFrame frame;
private Upload upload;
private JButton button;

public static void main(String[] args) throws Exception {

    AmazonS3 s3 = new AmazonS3Client(credentials = new ClasspathPropertiesFileCredentialsProvider().getCredentials());
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);
    tx = new TransferManager(s3);

    bucketName = "s3-upload-sdk-sample-" + credentials.getAWSAccessKeyId().toLowerCase();

    new S3TransferProgressSample();
}

public S3TransferProgressSample() throws Exception {
    frame = new JFrame("Amazon S3 File Upload");
    button = new JButton("Choose File...");
    button.addActionListener(new ButtonListener());

    pb = new JProgressBar(0, 100);
    pb.setStringPainted(true);

    frame.setContentPane(createContentPane());
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        JFileChooser fileChooser = new JFileChooser();
        int showOpenDialog = fileChooser.showOpenDialog(frame);
        if (showOpenDialog != JFileChooser.APPROVE_OPTION) return;

        createAmazonS3Bucket();

        ProgressListener progressListener = new ProgressListener() {
            public void progressChanged(ProgressEvent progressEvent) {
                if (upload == null) return;

                pb.setValue((int)upload.getProgress().getPercentTransfered());

                switch (progressEvent.getEventCode()) {
                case ProgressEvent.COMPLETED_EVENT_CODE:
                    pb.setValue(100);
                    break;
                case ProgressEvent.FAILED_EVENT_CODE:
                    try {
                        AmazonClientException e = upload.waitForException();
                        JOptionPane.showMessageDialog(frame,
                                "Unable to upload file to Amazon S3: " + e.getMessage(),
                                "Error Uploading File", JOptionPane.ERROR_MESSAGE);
                    } catch (InterruptedException e) {}
                    break;
                }
            }
        };

        File fileToUpload = fileChooser.getSelectedFile();
        PutObjectRequest request = new PutObjectRequest(
                bucketName, fileToUpload.getName(), fileToUpload)
            .withProgressListener(progressListener);
        upload = tx.upload(request);
    }
}

private void createAmazonS3Bucket() {
    try {
        if (tx.getAmazonS3Client().doesBucketExist(bucketName) == false) {
            tx.getAmazonS3Client().createBucket(bucketName);
        }
    } catch (AmazonClientException ace) {
        JOptionPane.showMessageDialog(frame, "Unable to create a new Amazon S3 bucket: " + ace.getMessage(),
                "Error Creating Bucket", JOptionPane.ERROR_MESSAGE);
    }
}

private JPanel createContentPane() {
    JPanel panel = new JPanel();
    panel.add(button);
    panel.add(pb);

    JPanel borderPanel = new JPanel();
    borderPanel.setLayout(new BorderLayout());
    borderPanel.add(panel, BorderLayout.NORTH);
    borderPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    return borderPanel;
    }
}
java jsp servlets
4个回答
3
投票

将java应用程序转换为jsp/servlet

根据程序的类型,它可能就像采用具有

main()
方法的类一样简单,使其成为
HttpServlet
的子类,然后采用
main()
方法中的所有内容并将其放入在 servlet 的
doGet() method
中。然后,您将所有
System.out.println()
语句替换为以 HTML 格式打印输出的语句。如果它只是一个没有
GUI (Graphical User Interface)
的命令行程序,那么这将起作用。

如果您的程序没有 GUI,那么您可以停止阅读此处。如果它确实有 GUI,那么它可能会复杂得多。根据程序的类型,您可以执行以下操作之一:

1。将整个内容放入 Applet 中。

This is pretty simple. Instead of replacing a few lines to turn the program into a Servlet, you'd be replacing a few lines to turn it into an Applet. 
This you could do if you don't need the server to communicate with, like if you had a solitare game. Otherwise, you could do #2.

2。将 GUI 放在 Applet 中,并将后端保留在服务器上。

This is what you'd call a "client/server" application.

3。创建一个基于 JSP/Servlet 的网站。

The most complicated option, but if you don't want to use Applets this is the only way to go.Here you're changing from a Java based GUI to an HTML/CSS based GUI. You might also need some JavaScript. If you're not familiar with all this stuff, but you're comfortable making Java GUI's using things like Swing, you might want to check out GWT (Google Web Toolkit). 
This allows you to make rich websites using plain Java. The Java code "compiles" into HTML and Javascript.

另一张:

它是在运行时完成的,即第一次调用 JSP 时。一些 Web 服务器还附带 JSP 编译器,允许在构建时执行此操作,这有两个优点:

  1. 它允许在构建时检测 JSP 语法错误,而不是 运行时。

  2. 它避免了第一次调用时间损失(需要一些时间 将 JSP 编译为 Java,然后将 Java 编译为字节码)。


2
投票

将 Java 代码放入 JSP 的做法已经被质疑了 10 年。在现代 JSP 中,使用 JSTL 和 EL 来代替不良做法的 Java 代码。

您可能会发现这篇文章有助于学习如何构建现代 Web 应用程序。


0
投票

您可以使用

JSP
提供 UI(即浏览和上传文件)。您可以使用多部分表单和 HTML 文件标签来实现相同的目的。更多相关内容这里

此外,您还可以使用

Servlet
将控制权委托给处理文件的适当业务类。

这样,你就能清楚地区分模型、视图和控制器。


0
投票

您在这里使用 swing 作为 GUI。您可以使用 JSP 进行视图或 GUI,而不是 swing GUI,通过使用 JSP,您可以捕获用户输入并在 JSP 上填充数据。为了处理业务逻辑,您可以使用 servlet。

希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.