12

How to invoke a simple servlet using the following URL: http://localhost:8080/servlet/MyServlet

I placed it in the folder: tomcat\webapps\ROOT\WEB-INF\classes

I've read there is no need to mention the servlet in web.xml. I did the same. Still, I'm unable to invoke it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Faisal
  • 645
  • 3
  • 11
  • 24

3 Answers3

27

I've read there is no need to mention the servlet in web.xml.

You're probably confusing with the legacy Tomcat-builtin InvokerServlet which was present in older versions of Apache Tomcat (and still mentioned in poor and outdated tutorials/books). It indeed allowed to invoke servlets like that without the need to map anything. However, it was later confirmed that it was a security hole and vulrenable to attacks. It was disabled and deprecated on Tomcat 5.0 and removed on Tomcat 7.0. In such case, you really need to map your servlet in web.xml (and put it in a package!).

Another source of confusion may be the new Servlet 3.0 @WebServlet annotation. When you're already using a Servlet 3.0 container like Tomcat 7.0, then you could use this annotation to map the servlet without the need to fiddle with web.xml.

package com.example;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

    // ...

}

Then you'll be able to access it the way you want.

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Absolutely! Thanks so much BalusC!! This was my second question and both the questions were answered by you. Thanks so much again!! – Faisal Feb 17 '11 at 16:17
  • Uhm, I don't see another question in [your profile](http://stackoverflow.com/users/618321/faisal)? Your account appears to be unregistered. I'd warmly recommend to register your account and then request the SO moderator team to merge your other unregistered account. This way you'll be able to use one and same user account from everywhere. – BalusC Feb 17 '11 at 16:19
  • @BalusC - Here is my another question: http://stackoverflow.com/questions/4737011/what-does-context-in-servletcontext-mean How to link this question with my account? many thanks in advance! – Faisal Feb 17 '11 at 16:43
  • You could do it by flagging the question for moderator attention. I've already done it for you. The other account will be merged soon :) – BalusC Feb 17 '11 at 16:46
  • find "What is Servlet Invoker?" in this page: http://javapapers.com/servlet/what-is-servlet-mapping/ – KNU Nov 05 '14 at 12:09
1

your web.xml file has to be like this

<web-app>

<servlet>
    <servlet-class>mypackage.myservlet</servlet-class> 
            <!--  the full name of your class  -->
    <servlet-name>name</servlet-name>
            <!-- name has be the same in servlet and servlet-mapping -->
</servlet>

<servlet-mapping>
    <servlet-name>name</servlet-name>
    <url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>

Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • 1
    I know how to do with web.xml! I read, there is no need of mentioning servlet mapping when they are placed in ROOT. – Faisal Feb 17 '11 at 15:39
-1

You can Achieve this in web sphere.By Enabling Serve Servlet By Class Name property,need follow below step to do this.

  1. Go to WebSphere Admin Console.
  2. Right Click on the WebSphere Server --> Admin Console.
  3. Click Servers --> Server Types --> WebSphere application servers --> server_name(name of your server name) --> Web Container Settings --> Web container.
  4. Set custom property com.ibm.ws.webcontainer.disallowServeServletsByClassname value as false.
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44