About Question enthuware.jwpv6.2.870 :

Moderator: admin

Post Reply
gurpreet_asrgndu
Posts: 55
Joined: Thu Jan 03, 2013 7:51 am
Contact:

About Question enthuware.jwpv6.2.870 :

Post by gurpreet_asrgndu »

if the getRequestDispatcher method returns null , shouldnt the call to forward throw NullPointerException.

or does the NPE gets wrapped by ServletException ??

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

Re: About Question enthuware.jwpv6.2.870 :

Post by admin »

Irrespective of whether a NPE is thrown or a ServletException is thrown, they will be caught by the catch(Exception) block.

HTH,
Paul.

gurpreet_asrgndu
Posts: 55
Joined: Thu Jan 03, 2013 7:51 am
Contact:

Re: About Question enthuware.jwpv6.2.870 :

Post by gurpreet_asrgndu »

oh thanks Paul.

GauravDaga
Posts: 1
Joined: Mon Mar 30, 2015 1:32 am
Contact:

Re: About Question enthuware.jwpv6.2.870 :

Post by GauravDaga »

Ok, I understood the relative path URL but in order for it to work.. question should clearly tell the location of login.jst with respect to current servlet. It doesn't add up :(

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

Re: About Question enthuware.jwpv6.2.870 :

Post by admin »

There is no need to know the location of login.jsp. No matter where it is kept, an exception will be thrown.

RumiJal
Posts: 9
Joined: Wed Jul 12, 2017 10:51 am
Contact:

Re: About Question enthuware.jwpv6.2.870 :

Post by RumiJal »

I understand that when using req.getRequestDispatcher("Login.jsp"); we get the ressource in a relative way. But relative to what exactly? The actual location is not mentioned and the request call is not clear so how we know that the last option is correct?

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

Re: About Question enthuware.jwpv6.2.870 :

Post by admin »

Relative to the current request's path. For example, if the current request is handled by /myjsp/someview.jsp, then Login.jsp should be in /myjsp directory.

In this question, however, no matter where you keep Login.jsp, the code will not work because ServletContext's getRequestDispatcher expects an absolute path. It cannot understand relative path (i.e. path not starting with a / ).

HTH,
Paul.

RumiJal
Posts: 9
Joined: Wed Jul 12, 2017 10:51 am
Contact:

Re: About Question enthuware.jwpv6.2.870 :

Post by RumiJal »

So when the servlet is invoked through the following url

http://localhost:8080/MyApp/

Then the answer is

It will compile and will work properly if //1 is replaced with: RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");

But if the servlet is invoked through the following url for example

http://localhost:8080/MyApp/something/

the answer should be
It will compile but not work properly if //1 is replaced with: RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");

Because the containter will try to find the Login.jsp in /MyApp/something/ directory. is that right?

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

Re: About Question enthuware.jwpv6.2.870 :

Post by admin »

That is correct.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests