How to deploy a servlet in Jetty?

I created a simple servlet that I want to deploy to Jetty 7.2. Jetty works and can serve JSP pages at http://localhost:8080/jonas/test.jsp . I started Jetty with the java -jar start.jar .

I saved the compiled servlet MyServlet.class to <my_jetty_directory>/webapps/jonas/WEB-INF/classes/MyServlet.class and then tried to access this servlet at http://localhost:8080/jonas/servlets/MyServlet , but I got an HTTP 404 error.

 HTTP ERROR 404 Problem accessing /jonas/servlet/MyServlet. Reason: Not Found 

Is there anything else I need to do? Where in the Jetty file structure should I place MySerlvet.class ?


Now I created a simple web.xml and saved it in <my_jetty_directory>/webapps/jonas/WEB-INF/web.xml and restarted my Jetty, but it does not 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, which is now resolved: How to deploy a JSP file on a Jetty web server?

+4
source share
2 answers

You must map your servlet in web.xml using <servlet> and <servlet-mapping> , or annotate it using @WebServlet when using servlet 3.0.

+4
source

I think there should be a config or readme.txt file in the installation folder or lib or bin or conf sub folders of your Jetty server. Read them and you will receive specific directories for placing your classes. Installing the class catalog on the Jetty server, or at least repeating it. These will be the classes that run when the Jetty server starts.

As for the classpath, java has a way to run classes from given 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 run the java command to search for classes in the current directory.

0
source

All Articles