5

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

5
  • 1
    Where is file.jsp located? Commented Jun 11, 2014 at 15:47
  • in the WEB-INF folder (it's the normal location in Eclipse) Commented Jun 11, 2014 at 15:53
  • Then you'll want to access it through /WEB-INF/file.jsp. Commented Jun 11, 2014 at 15:55
  • I've putted it under the folder WebContent and now it works. Now it's a stupid question :( Commented Jun 11, 2014 at 15:56
  • IS it typo <url-pattern>/urlpattern</url-pattern> or the exact url-pattern defined in web.xml. Is it HelloServlet.jsp or HelloServlet.java Commented Jun 11, 2014 at 16:05

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've putted it under the folder WebContent and now it works. Now it's a stupid question :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.