Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes J2eeCertificate ques Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "J2eeCertificate ques" Watch "J2eeCertificate ques" New topic
Author

J2eeCertificate ques

Sharma Anjali
Ranch Hand

Joined: Feb 22, 2005
Posts: 63
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




1.package com.baboon.servletmodel;
2.public class TestParamServlet extends HttpServlet {
3. protected void doPost(HttpServletRequest req,
4. HttpServletResponse resp)
5. throws ServletException, IOException
6. {
7. resp.getWriter().println(req.getParameter("param"));
8. }
9.}

Ans Given: Code compiles, a blank page is displayed.
Explanation:
If you enter an address in the address line of your browser then the HTTP GET method is triggered. The doGet method is not implemented and will use the default value of the HTTPServlet class.

When you want to trigger the HTTP GET method you should override the doGet method. When you have an HTML form and submit it to the server the doPost method is triggered.


But i tried writing a servlet with only a doPost method and accessed it by typing the url and this is what i got
HTTP Status 405 - HTTP method GET is not supported by this URL

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
Apache Tomcat/5.0.28


So is the above answer wrong?
[ March 01, 2005: Message edited by: Sharma Anjali ]
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
HI Anjali

Please try reading the books first before directly going for checking the output..

If you write method="Post" in the html form then only post method gets fired .Rest in all cases get method gets fired..


Regards
Rohit.
muthulingam Jayakanthan
Ranch Hand

Joined: Feb 27, 2005
Posts: 43
"Param=10" is different than "param=10". Look at the Case of the URL variable. That is y the answer gives blank
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
I think it will give you an error when you type the html link because the doGet() didn't define.
Sharma Anjali
Ranch Hand

Joined: Feb 22, 2005
Posts: 63
Originally posted by Rohit Bhagwat:
HI Anjali

Please try reading the books first before directly going for checking the output..

If you write method="Post" in the html form then only post method gets fired .Rest in all cases get method gets fired..


Regards
Rohit.


I think i didn't make my ques clear enough.

When a page is accessed by typing in a url, I know doGet() is called.
So doGet() will be called here.

But i don't have a doGet() defined here only a doPost()

Now the ques askes what will happen if a doGet() is called but there is no doGet() defined.

Explanation says you'll get a blank page because doGet() from super class will run. But when tried it I got a message (as posted above) and not a blank page.
Sharma Anjali
Ranch Hand

Joined: Feb 22, 2005
Posts: 63
Originally posted by Romy Huang:
I think it will give you an error when you type the html link because the doGet() didn't define.


Exactly that is what I got, but the explanation says it differently and I have seen the same explantion at a number of places.

Can someone plz confirm what should be the right answer.
Although it does look logical that doGet() from super class is called but then why does it give an error ?
Sharma Anjali
Ranch Hand

Joined: Feb 22, 2005
Posts: 63
Any one please
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
I think it may depend on the web container. I use tomcat and I would expect an error stating that doGet has not been defined...
-C


SCJP 1.4 SCWCD 1.4
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Exactly

I also get the error and also I think it is container dependent so I dont think that such questions might appear in the exam..

Worth asking a question in forum
Best of Luck
Regards
Rohit.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: J2eeCertificate ques