How do I deploy a Servlet in Jetty?

二次信任 提交于 2019-12-07 10:31:47

问题


I have created a simple Servlet that I want to deploy in Jetty 7.2. Jetty is running and is able to serve JSP pages on http://localhost:8080/jonas/test.jsp. I started Jetty with the java -jar start.jar command.

I saved my compiled Servlet MyServlet.class at <my_jetty_directory>/webapps/jonas/WEB-INF/classes/MyServlet.class and then tried to access that Servlet on http://localhost:8080/jonas/servlets/MyServlet but I get a HTTP 404 error.

HTTP ERROR 404

Problem accessing /jonas/servlet/MyServlet. Reason:

    Not Found

Is there something more I have to do? Where in the Jetty file structure should I place MySerlvet.class?


I have now created a simple web.xml file and saved it in <my_jetty_directory>/webapps/jonas/WEB-INF/web.xml and restarted my Jetty, but it doesn't work. Here is my simple web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
  </servlet>
</web-app>

I had a similar problem with JSP, that is solved now: How do I deploy a JSP file in the Jetty webserver?


回答1:


You have to map your servlet in web.xml, using <servlet> and <servlet-mapping>, or annotate it with @WebServlet if using servlet 3.0.




回答2:


I think there should be a config or readme.txt file in the installation folder or lib or bin or conf subfolders of your Jetty Server. Read through those and you will get the specific directories to put your classes. Setting your classes directory in the Jetty server or at least remeber it. Those will be the classes run when the Jetty server runs.

As for the classpath, java has a way of being told to run classes from specifies folders. You can add a-:

* "." at the end of classpath variable in WINDOWS NT platform
* set CLASSPATH=%CLASSPATH%;. in command mode or AUTOEXEC.bat of other WINDOWS
* set CLASSPATH=%CLASSPATH%:. and export CLASSPATH in linux

With this, "." - fullstop, in the classpath variable, you will make running the java command look for classes in that current direcory.



来源:https://stackoverflow.com/questions/4122629/how-do-i-deploy-a-servlet-in-jetty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!