0

RequestDispatcherTestI have this annoying problem with servlet request dispatcher not forwarding to my jsp. It is in the proper folder. Ive never had this before today and I have done this hundreds of times at school but for some reason today it is not working. Thank you all for the help. Here is the code

Servlet Code:

package testpack;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Test")
public class Test extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/testJsp2.jsp");
        rd.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

JSP Code:

<%@page import="java.util.*,testpack.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<p>This is a Test!!</p>

</body>
</html
Wiredo
  • 220
  • 3
  • 14
  • Could you show how you calling this servlet? – pratikpawar Dec 18 '15 at 23:28
  • You're saying that servlet is properly installed, deployed, initialized and running, and its `doGet()` method is successfully hit and executed? – BalusC Dec 19 '15 at 18:00
  • Not sure you need the "/" in /WEB-INF – galusben Dec 19 '15 at 18:02
  • I have uploaded an image to the link. I am just running it on eclipse as a Dynamic Web Project. It is supposed to go to the jsp page through the RequestDispatcher when I run the Servlet. All my old projects still work. But now when I make a new project it doesn't work. I asked my professor he said he is not sure what is causing this. – Wiredo Dec 19 '15 at 18:04
  • Tried it without the "/" in WEB-INF still doesn't work – Wiredo Dec 19 '15 at 18:08
  • OK I got it to work by removing "/WEB-INF/". That is really weird as it is in my WebContent folder and my other projects have the same path. Any ideas why this happened? Thanks everyone for the replies. I am a beginner programmer and this is my first post. – Wiredo Dec 19 '15 at 18:16
  • So you simply haven't physically placed the target JSP file in `/WEB-INF` folder? – BalusC Dec 19 '15 at 18:27
  • It is in the /WEB-INF folder you can see from the screenshot. All my other projects i've been using "/WEB-INF/filename.jsp" and it works. – Wiredo Dec 19 '15 at 18:45
  • So if you open the `/WEB-INF` folder, another `testJsp2.jsp` file will appear in its hierarchy? Namely, in your screenshot that folder is still closed. – BalusC Dec 19 '15 at 18:46
  • Ok you know you are rigt. The file was in WEB Content not WEB-INF. How stupid of me. Thanks guys – Wiredo Dec 19 '15 at 18:49

0 Answers0