Java – servlet mapping with URL patterns is not working and have to use URL-servlet/servletclassname for using servlets

javaservletsweb-hostingweb.xml

i have an web hosting space of java (jsp/servlet) and i have tried
many times servlets using mappings in web.xml file with its URL
patterns and when i used that URL then its showing message "The
requested URL /myservlet was not found on this server." page which is
default set by hosting provider. so when i asked it from hosting
provider that i am unable to use myservlet or any servlet that is
mapped in web.xml file then replied to me that "to use your servlet
please follow URL- www.yourdomain.com/servlet/myservlet" and when i
used this URL which i have not mapped in my web.xml file was working
and also i got many time that web.xml file is not using by the server

so i want to ask why its happening , i mean why web.xml file is not
working , why i have to use /servlet/servletclassname in order to use
the servlet and now how i can use URL pattern for dynamic URLs…

Please any buddy help me…!!!

Here is the Web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>MyPackage.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Best Answer

Are you using the correct context as the first part of your path in URL? Whenever you deploy an application, you specify a context that identify all your URLs. Any url-pattern will be applied after that context. Let's say you have context called "MyShop", then using your provided web.xml, you should call http://yourdomain/MyShop/MyServlet .

Related Topic