About Question enthuware.jwpv6.2.1090 :

Moderator: admin

Post Reply
himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

About Question enthuware.jwpv6.2.1090 :

Post by himaiMinh »

Hi, suppose my servlet can be access through this URL:
http://localhost:8080/dispatching/MyServletz,
where dispatching is context path, MyServletz is the servlet path.
I have html/copyright.jsp in my webapp root.
My file structure is :
|--dispatching
|--WEB-INF
|--classes
|--html
| -- copyright.jsp
My servlet code is like this:

Code: Select all

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServletz"})
public class MyServlet extends HttpServlet {
  @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
             request.getRequestDispatcher("../html/copyright.jsp").include(request, response);
    }
}
It returns a null pointer exception as ../html/copyright.jsp is not found.

But I tried this:

Code: Select all


@WebServlet(name = "MyServlet", urlPatterns = {"/MyServletz"})
public class MyServlet extends HttpServlet {
   @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
              request.getRequestDispatcher("html/copyright.jsp").include(request, response);
    }
}
It works and includes the copyright.jsp content.

admin
Site Admin
Posts: 10383
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1090 :

Post by admin »

What is your servlet path right now? Can you try with servlet path one level deeper?

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1090 :

Post by himaiMinh »

One more demo:

Code: Select all

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServletz/foo"})
public class MyServlet extends HttpServlet {

   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
              PrintWriter writer = response.getWriter();
             //localhost:8080/dispatching/MyServletz/foo
             request.getRequestDispatcher("../html/copyright.jsp").include(request, response);
  }
}
The URL to access the servlet is http://localhost:8080/dispatching/MyServletz/foo ,
where dispatching is the web application root.
To access the htm/copyright.jsp, the container does this:
1. erase foo from the URL, which is localhost:8080/dispatching/MyServletz
2. go to ../ , which is one directory up and becomes localhost:8080/dispatching
3. look for html/copyright.jsp , which is dispatching/html/copyright.jsp

admin
Site Admin
Posts: 10383
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1090 :

Post by admin »

So, as the question says, request.getRequestDispatcher("../html/copyright.jsp").include(request, response); could possibly work as expected. Right?

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1090 :

Post by himaiMinh »

The url "../html/copyright.jsp" works when the servlet path is in this pattern: /a/b.
Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests