• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sigh, more questions

 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in case you didn't guess, i spent today at my desk studying chapters 3-5 of the HF book, with no access to email, so here is my flood of other questions...

1) does the servlet spec guarantee that a no-arg constructor, and only a no-arg constructor, will be used to, er, construct servlet instances?

2) is there any drawback to having doGet call doPost just out of habit?

3) should you / must you call setContentType before writing to the output stream?

4) a redirect needs to use GET if there are parameters, yes?

5) in the web.xml, do the order of <servlet> and <context-param> matter? if so, do the orders of things in web.xml show up on the exams?

that's all for today...
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1) does the servlet spec guarantee that a no-arg constructor, and only a no-arg constructor, will be used to, er, construct servlet instances?


If i am writing the exam (1.3), i would say yes. Servlet container needs a public no arg constructor for instatiating the servlet class.


2) is there any drawback to having doGet call doPost just out of habit?


Definitely yes : One situation i could think of is, in auth-constraint element, we can set to which HTTP method that constraint should be applied. So if we set the constraint on doPost method and then if the client calls goGet, and if we have a doPost call in doGet, doPost still will be executed!


3) should you / must you call setContentType before writing to the output stream?


Its not mandatory, as contenttype has a default value for any response. But if it is binary or or any other format, then we need to.

4) a redirect needs to use GET if there are parameters, yes?


Sorry,Question is not clear!


5) in the web.xml, do the order of <servlet> and <context-param> matter? if so, do the orders of things in web.xml show up on the exams?


Even i have the same question. Just now Kathy replied that order does matter in 1.3, but not in 1.4

So it means, you shouldn't be getting any questions of that sort in 1.4
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question 3 *is* unclear, sorry. what i meant to say was if you do a sendRedirect and their are parameters, will the redirected-to URL have to take the parameters in GET-style (i.e on the command line)?

thanks for the other responses!
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heh heh, that's question *four*, not three.

not my day today...
 
Lakshmi Rama
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am also preparing for SCWCD. I will try to answer your questions as much as i know.

Originally posted by john guthrie:
in case you didn't guess, i spent today at my desk studying chapters 3-5 of the HF book, with no access to email, so here is my flood of other questions...

1) does the servlet spec guarantee that a no-arg constructor, and only a no-arg constructor, will be used to, er, construct servlet instances?

Yes. I read it in the specs that no-arg constructor will be used to instantiate a servlet.

2) is there any drawback to having doGet call doPost just out of habit?

3) should you / must you call setContentType before writing to the output stream?
setContentType is method allows us to specify the type of data we are sending in the response. If you plan to use PrintWriter instead of OutputStream, YES you must call setContentType() method before calling getPrintWriter() for the charset to take effect. If setContentType is not called then it is assumed as text/html.

4) a redirect needs to use GET if there are parameters, yes?

sendRedirect() asks the browser to get a resource from elsewhere... I am assuming its just passing URL and not URL with Parameters. For example if somebody enters wrong credentials then we redirect them to login page. Well i guess i am just learning.. I am interested to see whats the correct answer for this.

5) in the web.xml, do the order of <servlet> and <context-param> matter? if so, do the orders of things in web.xml show up on the exams?

Yes, Ofcourse the order matters. If you are setting any servlet context param name and value it should be configured before servlet tag. I don't know about how it comes in the exam...still have to face it


that's all for today...
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Sekhar's response is pretty good, but I would like to clarify a few points...



1) does the servlet spec guarantee that a no-arg constructor, and only a no-arg constructor, will be used to, er, construct servlet instances?


Yes, just like with JavaBeans.



2) is there any drawback to having doGet call doPost just out of habit?


When developing a web app I specifically design my servlets to respond to specific HTTP Methods; for example, I almost always use POST for forms. If the user types in the URL pattern for this servlet, I would then assume that the user did not initiate the request from the form, so I would redirect them to the form in the doGet method.

Actually, now I usually create Struts web apps and the ActionServlet handles both GET/POST without any distinction, so I have to code up the behaviour I just described by hand.



3) should you / must you call setContentType before writing to the output stream?


Technically, you may call setContentType after getting the response stream, but it is not a good practice and Lakshmi makes a good point that if you need a special character encoding, then you must setCharacterEncoding before calling getWriter.

However, there are deeper issues here that I would like to explain so you have a deeper understanding of what the container is doing. Clearly the response object (HttpServletResponse) has two aspects: headers and body. If you start writing to the body (via getWriter or getOutputStream) before setting your headers, then you run the risk that the response object's body buffer becomes full and is sent to the client. If that happens, then when you attempt to set a header or setContentType these methods will throw an IllegalStateException. Why? Because the headers must appear at the beginning of the HTTP stream, but your servlet has already commited the headers when the body buffer was flushed. Make sense?

Of course, all of this discussion is (mostly) moot if you are using an MVC architecture in which your servlets perform only controller code and you use JSPs for your views.



4) a redirect needs to use GET if there are parameters, yes?


This is really an HTTP spec question and will not appear on the exam. However, it is an interesting question. Check out the HTTP spec (IETF RFC 2068) at the web page RFC #2068.



5) in the web.xml, do the order of <servlet> and <context-param> matter? if so, do the orders of things in web.xml show up on the exams?


No, because the DD for servlet spec v2.4 uses a Schema that does not enforce order of the elements.

HTH,
Bryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic