• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet mapping question

 
Denis Berezhnoy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could you please help me? I am confused by the following question I found:

Question 28)

1)Given the following web application deployment descriptor:
<web-app>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
...
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

which statements are true?

1) servlet-mapping element should be inside servlet element
2) url-pattern can't be defined that way.
3) if you make the http call: http://host/servlet/Hello.do the servlet container will execute MyServlet.
4) It would work with any extension excepting jsp,html,htm

Answer 28)

3) If you make the http call: http://host/servlet/Hello.do the servlet container will execute MyServlet.
Try it.

My understanding was that only URLs which is ended with jsp extension will match this patten *.jsp.
Why does Hello.do match too?


Another question is what HTTP status will be returned to client in the following cases:

1. doGet is not overriden in servlet and GET request is received.
2. doPost is not overriden in servlet and POST is received.

In another words what is the default behaviour of the doXXX() methods in HttpServlet.

Thanks a lot!
Denis
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question seems strange, as no answer is correct.

1) servlet-mapping element should be inside servlet element
Wrong, servlet and servlet-mapping are two separated blocks.

2) url-pattern can't be defined that way.
Yes of course it can, it's an extension mapping (no leading slash, and ends with a dot and an extension)

3) if you make the http call: http://host/servlet/Hello.do the servlet container will execute MyServlet.
No, sorry it won't, MyServlet will be called only by using a .jsp ending request. The thing that happens here is that you replaced the default JSP pages handler in the container (if available). All jsp go to MyServlet. But .do will NOT go to your servlet. The "Try it" ending sentence made my wonder if it was really possible.. I tried and failed. It do not work.

Test : http://localhost:8080/test/Home.jsp
Result :
TestServ executed.

Test : http://localhost:8080/test/Home.do
Result :
Etat HTTP 404 - /test/Home.do
type Rapport d'�tat

message /test/Home.do

description La ressource demand�e (/test/Home.do) n'est pas disponible.
Apache Tomcat/5.5.15


4) It would work with any extension excepting jsp,html,htm
No, it will work only with jsp extension...

Other question now, default behavior for doXxx() methods you did not implement is to return an error saying the Http method is not supported.

Test result in tomcat :

Etat HTTP 405 - La m�thode HTTP POST n''est pas support�e par cette URL

type Rapport d'�tat

message La m�thode HTTP POST n''est pas support�e par cette URL

description La m�thode HTTP sp�cifi�e n'est pas autoris�e pour la ressource demand�e (La m�thode HTTP POST n''est pas support�e par cette URL).
Apache Tomcat/5.5.15


Et voila !

EDIT

Two more things :
First, there is a possible explanation why people who made this question made such a mistake. Maybe they had a default servlet mapping (<url-pattern>/</url-pattern) configured to call MyServlet, and in this case yes, the MyServlet would have been called by the .do request. But it would be strange coming from people making mock exams...

Second, and I'm really wondering about that, is where you found this question which has no correct answer? Can you tell me, please?
[ March 18, 2006: Message edited by: Frederic Esnault ]
 
Denis Berezhnoy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frederic,

Thanks a lot for explanations! I found this question here

http://www.javaranch.com/carl/SCWCD.htm

but as I can see now this question has been updated extension .do was replaced with *.jsp. Please see question 28.

Best regards,
Denis
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With .do replaced with .jsp, now it makes sense
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic