JSP – Run servlet in Tomcat without defining servlet mapping in web.xml

Running servlet in web application is required to define servlet mapping in web.xml.

Servlet mapping gives details to servlet engine where servlet class is located. And what is the URL path of the servlet.

Tomcat gives another feature to load servlet automatically with the help of servlet invoker. Servlet invoker invokes all servlet and make servlet mapping without defining in the WEB-INF/web.xml.

Servlet invoker can be started by uncommented in tomcat/config/web.xml file. It will load all servlet from particular default package.

    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>
          org.apache.catalina.servlets.InvokerServlet
        </servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

and servlet invoker mapping

    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>

Put all servlet in myApp/WEB-INF/classes folder

URL for these servlet will be

http://localhost:8080/myApp/servlet/YourServlet

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post