1

I have web.xml like this :

  <servlet>
    <servlet-name>MyDisplayCourse</servlet-name>
    <servlet-class>edu.itn.controller.MyDisplayCourse</servlet-class>
</servlet>

and servlet-mapping for the servlet is:

 <servlet-mapping>
    <servlet-name>MyDisplayCourse</servlet-name>
     <url-pattern>/admin/displaystudent</url-pattern>    
    <url-pattern>/displaystudent</url-pattern>    
 </servlet-mapping>

When i use :

 <url-pattern>/displaystudent</url-pattern>   

It finds the servlet MyDisplayCourse, but when i use append /admin/

 <url-pattern>/admin/displaystudent</url-pattern> 

This shows 404 error code in my web app. Can someone help me why doesn't support url like /admin/displaystudent but supports only single url like /displaystudent only.

12
  • Possible duplicate of many url-pattern for the same servlet Commented Feb 23, 2017 at 4:02
  • this is not the question i am asking. i want to use /admin/displaystudent instead of /displaystudent only. Commented Feb 23, 2017 at 5:03
  • How do you access your application? Can you provide the URL as well? Commented Feb 25, 2017 at 3:51
  • Both the urls should work fine. Can you create a sample project which can recreate the issue ? Commented Feb 25, 2017 at 8:16
  • @N00bPr0grammer localhost:8080/practiceweb/admin/displaystudent Commented Feb 25, 2017 at 8:32

1 Answer 1

1
+50

There is no issue with url-pattern

the issue is with your code in DisplayStudent.java

replace

RequestDispatcher rd=request.getRequestDispatcher("StudentTable.jsp");

with

RequestDispatcher rd=request.getRequestDispatcher("/StudentTable.jsp");

ServletRequestSpec

If the path begins with a "/" it is interpreted as relative to the current context root

otherwise it will concatinate with relative path in your case '/admin/StudentTable.jsp'

following code is implementation of getRequestDispatcher

@Override
public RequestDispatcher getRequestDispatcher(final String path) {
String realPath;
 if (path.startsWith("/")) {
    realPath = path;
 } else {
    String current = exchange.getRelativePath();
    int lastSlash = current.lastIndexOf("/");
    if (lastSlash != -1) {
        current = current.substring(0, lastSlash + 1);
    }
    realPath = CanonicalPathUtils.canonicalize(current + path);
 }
 return new RequestDispatcherImpl(realPath, servletContext);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.