Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet RequestDispatcher is not forwarding

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

like image 976
Kenny Avatar asked Sep 30 '22 14:09

Kenny


1 Answers

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.

like image 132
Braj Avatar answered Oct 04 '22 18:10

Braj