且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 14:11:10

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

Convert java application to jsp/servlet

根据程序的类型,它可以像取其中包含 main()方法的类,使其成为 HttpServlet 的子类,并将其中的所有内容都包含在内 main()方法并将其放在servlet的 doGet()方法中。然后,您将使用语句替换所有 System.out.println()语句,以HTML格式输出输出。如果它只是一个没有 GUI(图形用户界面)的命令行程序,这将有效。

Depending on what kind of program it is, it could be as easy as taking the class that has the main() method in it, making it a subclass of HttpServlet and taking everything that's in the main() method and putting it in the doGet() method of the servlet. Then you'd replace all the System.out.println() statements with statements to print the output in HTML. This would work if it's just a command line program without a GUI (Graphical User Interface).

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

If your program has no GUI then you can stop reading here. If it does have a GUI, then it could be a lot more complicated. Depending on what kind of program it is, you could do one of the following:

1。把整个东西放在Applet中。

1. Put the whole thing in an Applet.

这很简单。您可以替换几行来将其转换为Applet,而不是替换几行来将程序转换为Servlet。
如果你不需要服务器与之通信,你可以这样做,就像你有一个solitare游戏一样。否则,你可以做#2。

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

2. Put the GUI in an Applet and keep the back end on the server.

这就是你想要的调用客户端/服务器应用程序。

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

3. Create a JSP/Servlet based website.

最复杂的选项,但如果你不想使用Applet这是唯一的方法。在这里,您将从基于Java的GUI更改为基于HTML / CSS的GUI。您可能还需要一些JavaScript。如果您不熟悉所有这些内容,但是您很乐意使用Swing等Java GUI,那么您可能需要查看GWT(Google Web Toolkit)。
这允许您使用普通Java创建丰富的网站。 Java代码编译成HTML和Javascript。

另一个:

Another one:

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


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

  1. It allows detecting JSP syntax errors at build time rather than runtime.

它避免了第一次调用时间的惩罚(需要花费一些时间将
编译为Java,然后将Java编译为字节码)。

It avoids the first invocation time penalty (it takes some time compiling JSP to Java and then Java to bytecode).