Java Server using Jetty: Very simple example does not work, displays HTTP ERROR 503 Service Unavailable

huangapple go评论79阅读模式
英文:

Java Server using Jetty: Very simple example does not work, displays HTTP ERROR 503 Service Unavailable

问题

我一直在按照这个教程进行操作:https://happycoding.io/tutorials/java-server/servlets

显示静态HTML网页是可以的,但是我无法使动态版本正常工作。
我的web.xml文件如下:

<web-app>
    <servlet>
	    <servlet-name>MyHelloWorldServlet</servlet-name>
	    <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>
    <servlet-mapping>
	    <servlet-name>MyHelloWorldServlet</servlet-name>
	    <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
</web-app>

我的Java代码如下:

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.println("<h1>Hello world!</h1>");
		out.println("<p>This is our first servlet.</p>");
	}
}

我已经在BlueJ中编译了Java代码,并将.class文件添加到WEB-INF文件夹中的一个classes文件夹中。
我的文件顺序如下:
webapps/
HelloWorldWebApp/
WEB-INF/
web.xml
classes/
HelloWorldServlet.class

我不确定我做错了什么。

英文:

I have been following this tutorial: https://happycoding.io/tutorials/java-server/servlets

Displaying a static html webpage was fine, however I can't get the dynamic version to work.
My web.xml is as follows:

&lt;web-app&gt;
    &lt;servlet&gt;
	    &lt;servlet-name&gt;MyHelloWorldServlet&lt;/servlet-name&gt;
	    &lt;servlet-class&gt;HelloWorldServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
	    &lt;servlet-name&gt;MyHelloWorldServlet&lt;/servlet-name&gt;
	    &lt;url-pattern&gt;/index.html&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;

My java code is as follows:

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.println(&quot;&lt;h1&gt;Hello world!&lt;/h1&gt;&quot;);
		out.println(&quot;&lt;p&gt;This is our first servlet.&lt;/p&gt;&quot;);
	}
}

I have compiled the java code in BlueJ and added the .class file to a classes folder in WEB-INF.
My file order is as follows:
webapps/
HelloWorldWebApp/
WEB-INF/
web.xml
classes/
HelloWorldServlet.class

I'm not sure what I'm doing wrong.

答案1

得分: 0

我找到了问题。

浏览器只显示了503错误,但我启动服务器的终端实际上显示了更多的错误信息。错误是我使用的当前版本的jdk与jetty不兼容。我无法弄清楚如何在BlueJ中更改java版本,所以我只是下载了SE 8并通过命令行进行了编译。这解决了问题。

英文:

I figured out the problem.

The browser was only displaying the 503 error, but the terminal from which I had started the server was actually displaying more error information. The error was that the current version of jdk I was using was not compatible with jetty. I couldn't figure out how to change the java version in bluej, so instead I just downloaded se 8 and compiled through the command line. This fixed the problem.

huangapple
  • 本文由 发表于 2020年8月22日 02:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定