Servlet RequestDispatcher is not forwarding
Try with prefix slash as shown below
RequestDispatcher aDispatcher = request.getRequestDispatcher("/file.jsp");
if jsp file is present directly under webapp folder.
or try
RequestDispatcher aDispatcher = request.getRequestDispatcher("/WEB-INF/file.jsp");
if jsp file is under WEB-INF folder.
project structure:
WebContent
|
|__file.jsp
|
|__WEB-INF
|
|__file.jsp
|__web.xml
Read What is WEB-INF used for in a Java web application?
If you want not to access this JSP file directly then put is inside the WEB-INF
folder that can't accessed publically that is more secure way for restricted resources.
A JSP file placed under WEB-INF can't accessed directly by simply hitting the URL in that case it can be accessed by the application only.
Kenny
Hey I'm a student Informatics - Computer and Cyber Crimes and I want to learn as much as possible
Updated on June 12, 2022Comments
-
Kenny almost 2 years
I'm learning Java Servlets and JSP.
I have the following code:
HelloServlet.jsp
public class HelloServlet extends HttpServlet { private static final long serialVersionUID=1; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8); RequestDispatcher aDispatcher = request.getRequestDispatcher("file.jsp"); aDispatcher.forward(request,response); } }
file.jsp
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>First JSP</title> </head> <body> Hello!! </body> </html>
My
web.xml
looks like this:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:schemaLocation="http://java.sun.som/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Hello</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>Hello Servlet</display-name> <servlet-name>hello</servlet-name> <servlet-class>be.howest.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/urlpattern</url-pattern> </servlet-mapping> </web-app>
When I'm running the file on Tomcat, I get the following error:
HTTP Status 404 - /Projectname/file.jsp
type - Status report message - Projectname/file.jsp description - The requested resource is not available.
What did I do wrong? because I can't find the solution by myself
-
Sotirios Delimanolis almost 10 yearsWhere is
file.jsp
located? -
Kenny almost 10 yearsin the
WEB-INF
folder (it's the normal location in Eclipse) -
Sotirios Delimanolis almost 10 yearsThen you'll want to access it through
/WEB-INF/file.jsp
. -
Kenny almost 10 yearsI've putted it under the folder WebContent and now it works. Now it's a stupid question :(
-
Braj almost 10 yearsIS it typo
<url-pattern>/urlpattern</url-pattern>
or the exact url-pattern defined in web.xml. Is itHelloServlet.jsp
orHelloServlet.java
-
-
Kenny almost 10 yearsI've putted it under the folder
WebContent
and now it works. Now it's a stupid question :(