1

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sam
  • 73
  • 3
  • 10
  • Without seeing your web.xml, the only thing that anybody can give you is general advice. If you want something better, edit your question and post your `` and `` sections. The *exact* sections, copied an editor window, not some made-up versions that you think represent what's actually there. – kdgregory Dec 08 '12 at 12:29
  • ok so here is the Web.xml file – sam Dec 08 '12 at 14:30

3 Answers3

2

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 .

eppesuig
  • 1,375
  • 2
  • 12
  • 28
  • ok after suggesting by you i have type url in browser www.domain.com/ServletMappingTest/MyServlet this and didn't any change in web.xml means mapping url /MyServlet was as it is but it didn't work please any other way suggest me – sam Dec 10 '12 at 09:23
  • If you specify an URL that is not mapped, you shoud get an error from tomcat. What does this error says? – eppesuig Dec 10 '12 at 11:03
  • Not Found The requested URL /ServletMappingTest/MyServlet was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. – sam Dec 10 '12 at 13:05
  • here i am giving url by this you can see the if is there any erorr please tell me or you can also give me any of these type testing project in which mapping is done acc to you so here i will upload that and check and reply to you...thanks for helping...!!! – sam Dec 10 '12 at 13:08
  • please download it from the above given URL – sam Dec 10 '12 at 13:12
  • The project works perfectly. Are you sure you use the correct hostname and port? In my case tomcat is listening on port 8080, so the URL to reach it is http://localhost:8080/ServletMappingTesting/ – eppesuig Dec 10 '12 at 13:50
  • Yes, Project is properly working on my localhost machine by putting the URL localhost:8080/ServletMappingTesting but when i am uploading this project on a web hosting server so there is problem to call servlet by URL mapped in web.xml as i have told in my question...!!! – sam Dec 10 '12 at 16:27
  • to call the servlet on that server hosting provider told me to put URL servlet/servletclassname to call any of servlet – sam Dec 10 '12 at 16:28
  • so i don't know why i have to use only this type of pattern which is static and i am putting URL in web.xml so no changes is happening like apache is not using web.xml...or something like – sam Dec 10 '12 at 16:30
  • Probaly your hosting provider use some sort of reverse proxy and is rewriting the URLs it receive before calling tomcat. In this case you have to better ask your provider and get his rewriting details. – eppesuig Dec 10 '12 at 17:57
0

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • actually i am using Tomcat 7 and the hosting server is also providing the same Tomcat 5.5.X – sam Dec 08 '12 at 14:37
0

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)

Gideon
  • 1
  • 1