Hi all , I am trying to map a user to a jsp page when he tries to enter to a directory of my application using URL. let say my web directory is called school and it has a directory called pages . now I need to map a user to error page when he tries to access to pages directory using URL
something like this:
http;//ip_address/school/pages
I have done this in web.xml but it is not working why!!!:

<servlet>
<servlet-name>myjsp</servlet-name>
<jsp-file>/school/errorPage.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>myjsp</servlet-name>
<url-pattern>/school/pages</url-pattern>
</servlet-mapping>

but it is not working ,
please tell me if I am wrong and how I correct it , please if you have another idea please tell me
I need it to apply it in my application
thanks

Dani AI

Generated

The problem is that your web.xml mappings and file paths should be relative to the web application (context) root — not to the server filesystem or the context path you type in the browser. was right that the JSP must be inside the deployed webapp directory (for example webapps/school/errorPage.jsp), and was right to point out that you should not include the context name (/school) in the url-pattern. On CentOS the filesystem is case-sensitive, so make sure filenames and paths match exactly. Also check server logs after redeploy; XML errors or deployment failures will show there.

Example of a correct servlet/JSP mapping (placed in WEB-INF/web.xml of the "school" webapp — note the absence of "/school" in patterns and in the jsp-file path):

<servlet>
  <servlet-name>myjsp</servlet-name>
  <jsp-file>/errorPage.jsp</jsp-file>
</servlet>

<servlet-mapping>
  <servlet-name>myjsp</servlet-name>
  <url-pattern>/pages/*</url-pattern>
</servlet-mapping>

Use /pages/* to catch requests to /pages, /pages/ and any sub-paths. After editing web.xml, redeploy or restart the app so the container picks up the changes.

If you only need to handle a browser request that targets the directory (not files inside it), an easier approach is to add a simple index.jsp inside the pages folder that forwards or redirects to your error page (this avoids servlet mapping complexity). Example (put as pages/index.jsp):

<% response.sendRedirect(request.getContextPath() + "/errorPage.jsp"); %>

Troubleshooting checklist: confirm web.xml is in WEB-INF and well-formed; verify the JSP is in the deployed WAR (not just your source tree); check file name case; check file permissions and SELinux if CentOS is enforcing; watch container logs for deployment or mapping errors.

Recommended Answers

All 3 Replies

File errorPage.jsp must be placed inside school folder in the root of your website.

root\
         |
         \school\
                  |
                  | errorPage.jsp

I have done what you have said but still doesn't work although I save configuration after modification of web.xml

I would tell that my server is running on Centons , is it the problem ?
pleasse I need help with this

What is the name of your web application? If it's school, then trying removing /school from the URL pattern. It should work. Also, specify the location of all your resources wrt the web app root i.e. exclude the `school' directory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.