4

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?

Community
  • 1
  • 1
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    Are you using Jetty as standalone servletcontainer to which you deploy fullworthy WAR files? Or are you using Jetty as embedded servletcontainer and programmatically creating/starting it by Java code? – BalusC Nov 08 '10 at 11:40
  • @BalusC: I'm using Jetty as a standalone servletcontainer, and I'm only deploying a simple "Hello World" servlet class, I haven't learnt about how to create `.war` files yet. I will learn the basics first. – Jonas Nov 08 '10 at 12:28
  • 1
    OK, then Bozho has it indeed correct. Otherwise the answer would have been `context.addServlet(new ServletHolder(new MyServlet(), "/myservlet"))` as outlined here http://docs.codehaus.org/display/JETTY/Embedding+Jetty – BalusC Nov 08 '10 at 13:09

2 Answers2

4

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I think Jetty only has support for servlet 2.5. I created a `web.xml` file as you suggested. However, I don't really know how it should look like and be placed. I'm reading from a book "Murach's Java Servlets and JSP" from 2003. You suggest other tags. My `web.xml` didn't solve this problem. – Jonas Nov 08 '10 at 12:26
  • 1
    @Jonas you showed only `` and no `` - add a mapping as well (this defines which URL maps to which servlet) – Bozho Nov 08 '10 at 12:30
  • My book was to old and didn't have `` but now I have learn about it, and now it works great. Thanks again. – Jonas Nov 08 '10 at 12:51
  • 2
    Throw that book away and read [servlet tag info page](http://stackoverflow.com/tags/servlets/info) for better links/tutorials/books. – BalusC Nov 08 '10 at 13:10
  • @Abhishek wow ! I tried to edit and add a short example and guess what I get "This edit is incorrect or an attempt to reply to or comment on the existing post." – Harry Sep 04 '14 at 03:57
  • @HarendraSingh You might want to add a new answer to post your example. In that answer, you should perhaps acknowledge this answer. – Abhishek Anand Sep 04 '14 at 14:38
0

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.

Muhammad Shahab
  • 4,187
  • 4
  • 34
  • 44