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

Exam question.

 
Rohit Kelkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Review Questions & Answers
Question 27
The contents of form.html are
<html>
<form method=post action="http://manoj:8080/servlet/myservlet">
Name<input type=text name="name">
<input type=submit value=click>
</form>
The contents for myservlet are
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class myservlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
PrintWriter out=res.getWriter();
out.println("Name is "+req.getParameter("name"));
}
}
Suppose that user enters name as SCWCD,
Select the correct output generated

A Prints Name is SCWCD
B Prints Name is null
C gives compilation error
D Prints Name is
Correct Ans is C. but I thought HttpServlet gives a empty body implementation for each of the doXXX methods !!!> So actually according to be none of them are valid....
 
AJAY RANA
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rohit,
I think both the answer and ur explanation are correct.The compilation error which u see is generated by Tomcat engine because it could not find doPost() method i.e. the error is specific to tomcat.
Regards,
Ajay Rana
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rohit,
You are right. The abstract class HttpServlet has default implementations for all 7 HTTP methods GET , POST, HEAD, PUT, DELETE, TRACE, OPTIONS.
But, out of these 7 methods only 3 of them doHead(req,res), doTrace(req,res) , doOptions(req,res) are implementated in such a way that we need not override their functionality. The default implemetation of the other 4 HTTP methods doGet,doPost,doPut,doDelete is to throw a 'BAD REQUEST ERROR'.
This is what you are seeing. If we want to use GET , POST, PUT ,DELETE http methods, then we must override doGet, doPost, doPut methods.
This point is not clearly said even in Servlet API or Sun's servlet tutorial. Please refer to this link for more information.
http://www.novocode.de/doc/servlet-essentials/chapter1.html
Regards,
Maha Anna
[ October 27, 2002: Message edited by: Maha Annadurai ]
 
friso jonge
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can it be a compilation error accoring to Ajay Rana the tomcat engine pics it up ?
Would that be a compilation error on the JSP ?
Also if we agree with maha, then it would give a bad request error page because of the default implementation and still not a compilation error ?
please explain.
thanks,
friso
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I overlooked the 'compilation' part. It is definitly not a compilation error. It is a server Bad Request Error. Not sure what the author meant by 'Compilation Error'.
I just checked the sample code posted by Rohit in Tomcat. This what we get.
Apache Tomcat/4.0.3 - HTTP Status 405 - HTTP method POST is not supported by this URL
--------------------------------------------------------------------------------
type Status report
message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).
*********************************************
The default implementations of doGet,doPost,doDelete,doPut are to send an error with a message of "HTTP method **** ( where **** will be GET/POST/DELETE/PUT) is not supported by this URL"
Check this link on servlet API
http://java.sun.com/products/servlet/2.1/api/javax.servlet.http.HttpServlet.html#_top_
[ October 27, 2002: Message edited by: Maha Annadurai ]
 
Rohit Kelkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for the replies guys.
Those explainations seem to fit
 
Rohit Kelkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for the replies guys.
Those explainations seem to fit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic