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>
2
  • 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 <servlet> and <servlet-mapping> sections. The exact sections, copied an editor window, not some made-up versions that you think represent what's actually there. Commented Dec 8, 2012 at 12:29
  • ok so here is the Web.xml file Commented Dec 8, 2012 at 14:30

3 Answers 3

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 .

Sign up to request clarification or add additional context in comments.

10 Comments

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
If you specify an URL that is not mapped, you shoud get an error from tomcat. What does this error says?
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.
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...!!!
please download it from the above given URL
|
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.

1 Comment

actually i am using Tomcat 7 and the hosting server is also providing the same Tomcat 5.5.X
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.