Wednesday, October 24, 2007

Java SERVLET Interview Questions-Part4


Question: Request parameter How to find whether a parameter exists in the request object?

Answer: 1.boolean hasFoo = !(request.getParameter("foo") == null || request.getParameter("foo").equals(""));

2. boolean hasParameter = request.getParameterMap().contains(theParameter);

(which works in Servlet 2.3+)

Question: How can I send user authentication information while makingURLConnection?

Answer: You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

Question: What is the Max amount of information that can be saved in a Session Object ?

Answer: As such there is no limit on the amount of information that can be saved in a Session Object. Only the RAM available on the server machine is the limitation. The only limit is the Session ID length(Identifier) , which should not exceed more than 4K. If the data to be store is very huge, then it's preferred to save it to a temporary file onto hard disk, rather than saving it in session. Internally if the amount of data being saved in Session exceeds the predefined limit, most of the servers write it to a temporary cache on Hard disk.

Question: Can we use the constructor, instead of init(), to initialize servlet?

Answer: Yes , of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

Question: How can a servlet refresh automatically if some new data has entered the database?

Answer: You can use a client-side Refresh or Server Push

Question: The code in a finally clause will never fail to execute, right?

Answer: Using System.exit(1); in try block will not allow finally code to execute.

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com