This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in Mock exam qn

 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken the following question from J2EE certification Mock exam

package com.baboon.servletmodel;
public class TestParamServlet extends HttpServlet
protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException
{
resp.getWriter().println(req.getParameter("param"));
}
}


What would be the output in your browser when you invoke the servlet-code with the following URL:
http://localhost/servlet/com.baboon.servletmodel.TestParamServlet?Param=10

The answer is given as it will just print a blank page. Is'nt is supposed to print an error telling getMethod is not defined ?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

resp.getWriter().println(req.getParameter("param"));
}
}


What would be the output in your browser when you invoke the servlet-code with the following URL:
http://localhost/servlet/com.baboon.servletmodel.TestParamServlet?Param=10

The answer is given as it will just print a blank page. Is'nt is supposed to print an error telling getMethod is not defined ?


It really print blank page. I don't understand the getMethod specified by you. The only thing in the question is the Request Parametsers are CASE SENSATIVE.

Thanks
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
resp.getWriter().println(req.getParameter("param")); //here 'param'

http://localhost/servlet/com.baboon.servletmodel.TestParamServlet?Param=10 //here 'Param'

is it OK???

Thanks.
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the user types an url the method invoked is doGet()
Now suppose doGet is not overriden wont it give an error at run time telling method doGet() is not defined.

I quote the following from HFSJ pg :125

if an HTML form doesnt explicity say method="POST" the request is sent as a GET ,not a POST.If u dont have a doGet() in ur servlet the request will fail
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Additionally, The doPost method is never invoked by the given URL. So, it will throw the exception when request is passed to this servlet.

Thanks
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Thats Precisely what i thought. An excpetion has to be thrown. But in J2EE certificate the answer given is that a blank page will be displayed . They reason that the the default doGet() method in HttpServlet will be invoked and since that method is defined to be empty it simply doesnt do anything resulting in a blank page being displayed.
So which answer is correct
1) an exception is thrown
2) A baln page with no value is printed
???
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

They reason that the the default doGet() method in HttpServlet will be invoked and since that method is defined to be empty it simply doesnt do anything resulting in a blank page being displayed.


Atleast for Tomcat the default implementation for doGet() method is not emplty. It set "HTTP Status 405 - HTTP method GET is not supported by this URL" error code.



Thanks
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for ur reply narendra
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are we not suppose to include forward slash "/" instead of "." dot to include package structure in the url?
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi veena,

The question is from old exam. The old versions of tomat accept such URL. without servlet mapping.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic