1

this is my path

 <welcome-file>home</welcome-file>

and my servlet ,I declare urlpattern belows

@WebServlet(urlPatterns="/home")

and forward to file homeview.jsp

        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/SimpleWebApp/WebContent/WEB-INF/views/homeview.jsp");
dispatcher.forward(request, response);

but when i run my web app it error with htttp status 404 anyboby,help me fix it?Thank alot

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hung Huy
  • 15
  • 4

2 Answers2

2

Your code is working correctly. But,

Like the following in the project structure, you can access it this way.

HomeController (yourServlet)

request.getRequestDispatcher("/WEB-INF/views/homeview.jsp").forward(request, response);

Structure

WebContent
       |
       |__static
       |
       |__WEB-INF
              |__lib
              |__views/homeview.jsp
              |__web.xml
Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21
  • You're absolutely right. I try it and it works on Jetty. I speak a little English.the writing is worse:) I'm sorry. I'm not writing to try it myself. – Gurkan Yesilyurt Dec 14 '15 at 15:25
  • Thank,But Im not use Jetty,Im just learn servler and jsp about several day,and i dont know how to use jetty.and i want use anotation – Hung Huy Dec 14 '15 at 15:28
0

A welcome file can only be a static file, so for example a jsp or html file, it cannot specify a servlet.

If you want a servlet to act as a welcome page you should set its mapping to /* which makes it a default servlet. So in your annotation:

@WebServlet(urlPatterns="/*")

This way the servlet will be called whenever a request is received which does not map to any other resources.

Also you need to fix your forward request to specify the url relative to the content root of the app as shown in the other answer.

For reference this is how mapping works according to the servlet spec (this text taken from Section 12.1 of the Servlet 3.0 specification) :

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.

  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.

  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.

If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

mmulholl
  • 281
  • 2
  • 7
  • Im fixed done.error not in code and structure project.I checked my homview.jsp and found it error on here (I include _fooder,and _header for homeview but it error ).Thank all ! – Hung Huy Dec 18 '15 at 15:12