不使用 Servlet 和 Tomcat 使用 Java 创建一个简单的网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11215120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:16:36  来源:igfitidea点击:

Creating a simple web page using Java without using Servlet and Tomcat

javaservletswebpagejava-server

提问by js0823

I want to create a simple server application which runs on the desktop, and when I type my ip and port on the web browser, it connects to the server client which then opens a webpage with appropriate displays coded on the server application.

我想创建一个在桌面上运行的简单服务器应用程序,当我在 Web 浏览器上输入我的 ip 和端口时,它会连接到服务器客户端,然后服务器客户端打开一个网页,并在服务器应用程序上编码适当的显示。

I read online that I need to use servlet and Apache Tomcat to make a webpage using Java.

我在网上看到我需要使用 servlet 和 Apache Tomcat 来制作使用 Java 的网页。

I am wondering if there are any easier way to make a simple webpage which can contain buttons without using servlet and Apache Tomcat?

我想知道是否有更简单的方法来制作一个简单的网页,它可以在不使用 servlet 和 Apache Tomcat 的情况下包含按钮?

For example, I can use sockets to communicate between server and client applications. Could I change this client into typing the ip address and port on the web browser which will display a webpage created and contained in the server application and remove the need for servlet and tomcat? If so, how do I create a button on the server application so that web browser can see the button when connecting to the server application?

例如,我可以使用套接字在服务器和客户端应用程序之间进行通信。我可以将此客户端更改为在 Web 浏览器上键入 IP 地址和端口,这将显示在服务器应用程序中创建和包含的网页,并消除对 servlet 和 tomcat 的需要吗?如果是这样,我如何在服务器应用程序上创建一个按钮,以便在连接到服务器应用程序时 Web 浏览器可以看到该按钮?

Thank you very much.

非常感谢你。

回答by Tom

If I understand you correctly, you want to have a web page, but you don't want to use Tomcat (or any other servlet engine).

如果我理解正确,您想要一个网页,但不想使用 Tomcat(或任何其他 servlet 引擎)。

Although it is technically possible to write your own little web server (using server sockets etc), but what you're basically doing then is rewriting Tomcat. Writing a good web server is a daunting job, and should not be taken lightly. I think you are underestimating that. Instead, use what is already there. Tomcat is really quite easy get running.

尽管在技术上可以编写自己的小型 Web 服务器(使用服务器套接字等),但是您基本上要做的是重写 Tomcat。编写一个好的 Web 服务器是一项艰巨的工作,不应掉以轻心。我认为你低估了这一点。相反,使用已经存在的东西。Tomcat 真的很容易上手。

回答by tjg184

Creating the server piece could be done with raw sockets, but I would look at at an embedded server like Jetty. I think it will save you a lot of time and headache.

创建服务器部分可以使用原始套接字完成,但我会考虑像 Jetty 这样的嵌入式服务器。我认为它会为您节省大量时间和头痛。

http://docs.codehaus.org/display/JETTY/Embedding+Jetty

http://docs.codehaus.org/display/JETTY/Embedding+Jetty

回答by Peter Di Cecco

You should look into Play Framework. It will be easier then implementing Tomcat or Apache.

你应该看看Play Framework。实现Tomcat或Apache会更容易。

Version 1.2.4 is stable and feature-complete for Java, version 2.x is focused on Scala and doesn't have all the features of 1.2.4 yet.

1.2.4 版对于 Java 来说是稳定且功能完备的,2.x 版专注于 Scala,还没有 1.2.4 版的所有功能。

回答by FSP

You don't have to create a servlet. But, you need something that can parse a jsp page - it could be tomcat or some other server which has the same capabilities as tomcat. Though I am not sure if I understood your question correctly.

您不必创建 servlet。但是,您需要一些可以解析 jsp 页面的东西——它可以是 tomcat 或其他一些与 tomcat 具有相同功能的服务器。虽然我不确定我是否正确理解了你的问题。