0

I have a simple servlet program printing Hello World. I have set up Tomcat in Eclipse. When I click 'Run on Server' in Eclipse, it opens an in-built browser and prints the output.

Now, I tried to add a web.xml file and made the following entries:

<web-app>
<servlet>
     <servlet-name>test</servlet-name>
     <servlet-class>Simple_Servlet/HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>test</servlet-name>
     <url-pattern>/do</url-pattern>
</servlet-mapping>
</web-app>

The URL pattern is /do. I am trying to do /do after localhost:8080 but it doesn't work. How should I access the servlet?

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Prasanna
  • 85
  • 2
  • 2
  • 11
  • I am actually doing http://localhost:8080/do... I intentionally added a space between local and host in my post because I wasn't able to post it as localhost:8080 – Prasanna Aug 23 '14 at 01:15
  • 1
    I think your servlet class field should be a fully qualified class name. It should be `com.domain.something.Servlet` and not `com/domain/something/Servlet` – Edwin Dalorzo Aug 23 '14 at 01:15
  • I think @EdwinDalorzo is right, but as a sidenote, normally web.xml will be generated by the IDE if you select the proper project type. It's always best to select the correct project type and leave the housekeeping to the IDE. Or switch to EE6 and just forget about web.xml in most cases. – fvu Aug 23 '14 at 01:19
  • Thank you for your responses. My package is Simple_Servlet and my class HelloWorldServlet.java. So I have Simple_Servlet/HelloWorldServlet. What should I add to this? – Prasanna Aug 23 '14 at 01:26
  • Your syntax is not correct if your package name is **Simple_Servle** then you can't use like this `Simple_Servlet/HelloWorldServlet` you should replace that line of code with this `Simple_Servlet.HelloWorldServlet` then it will access this servlets. – Sandeep Roniyaar Aug 23 '14 at 06:42
  • @Prasanna: when you cannot put a string in a post, say it is code ... – Serge Ballesta Aug 23 '14 at 07:13

1 Answers1

1

Change <servlet-class>Simple_Servlet/HelloWorldServlet</servlet-class> to <servlet-class>Simple_Servlet.HelloWorldServlet</servlet-class>

More info on this: http://oreilly.com/pub/a/java/archive/tomcat.html?page=4

Jaec
  • 390
  • 2
  • 8
  • 18
  • Thank you, this works. Sorry, I had mentioned wrongly that Simple_Servlet was my package name. It is actually the name of my project; inside that is the class HelloWorldServlet. Should the browser always access using /Simple_Servlet/URL-pattern? Or is there a way to skip the project name and go straight to the servlet? – Prasanna Aug 23 '14 at 13:08
  • There is a way: http://stackoverflow.com/questions/5328518/deploying-my-application-in-tomcat-in-the-root – Jaec Aug 24 '14 at 17:51