英文:
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:
<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>
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("<h1>Hello world!</h1>");
out.println("<p>This is our first servlet.</p>");
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论