sam
sam

Reputation: 83

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

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>

Upvotes: 1

Views: 10363

Answers (3)

Gideon
Gideon

Reputation: 1

I got the same issue and figured out the reason was because of Tomcat (9.0.73) does not support Jakarta.servlet.api (5.0.0). jakarta.servlet.api seems only supported by Tomcat version >= 10. I'm using IDEA Ultimate, creating Java-EE application project is fine, however, IDEA will automatically add "Jakarta-servlet-api" version 5.0 in pom.xml file, in that case, the server will start normally and you would not get any error, however servlet-mapping (or even @WebServlet annotation) would not work and result in 404 Error. The solution is to replace Jarkata-servlet-api by a suitable version of servlet-api (in my case it was javax.servlet.api version 4.0.1)

Upvotes: 0

eppesuig
eppesuig

Reputation: 1385

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 .

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109532

Try it first at home. The NetBeans IDE (for instance) has a good server integration. You could download it with packaged Glassfish (or Tomcat) and experiment there.

There are many pieces to put together, so I would recommend experimenting yourself. Especially writing out getServletContextPath and all those partial path getters.

Check in web.xml

  • web-app / display-name
  • web-app / servlet-mapping / url-pattern+

Also with autodeployment the war name may matter.

Upvotes: 0

Related Questions