Friday, June 25, 2010

SERVLET CONTINUES ... 1

QUESTION: Do objects stored in a HTTP Session need to be serializable? Or can it store any object?
ANSWER: Yes, the objects need to be serializable, but only if your servlet container supports persistent sessions. Most lightweight servlet engines (like Tomcat) do not support this. However, many EJB-enabled servlet engines do. Even if your engine does support persistent sessions, it is usually possible to disable this feature.

QUESTION: What is the difference between session and cookie?
ANSWER: The difference between session and a cookie is two-fold.
1) session should work regardless of the settings on the client browser. even if users decide to forbid the cookie (through browser settings) session still works. there is no way to disable sessions from the client browser.
2) session and cookies differ in type and amount of information they are capable of storing.
Javax.servlet.http.Cookie class has a setValue() method that accepts Strings. javax.servlet.http.HttpSession has a setAttribute() method which takes a String to denote the name and java.lang.Object which means that HttpSession is capable of storing any java object. Cookie can only store String objects.

QUESTION: What is the difference between ServletContext and ServletConfig?
ANSWER: Both are interfaces.
The servlet engine implements the ServletConfig interface in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init() method.
The ServletContext interface provides information to servlets regarding the environment in which they are running. It also provides standard way for servlets to write events to a log file.

QUESTION: What are the differences between GET and POST service methods?
ANSWER: A GET request is a request to get a resource from the server. Choosing GET as the "method" will append all of the data to the URL and it will show up in the URL bar of your browser. The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. A POST request is a request to post (to send) form data to a resource on the server. A POST on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar. You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects!
QUESTION: What is the difference between GenericServlet and HttpServlet?
ANSWER: GenericServlet is for servlets that might not use HTTP, like for instance FTP service.As of only Http is implemented completely in HttpServlet.

The GenericServlet has a service() method that gets called when a client request is made. This means that it gets called by both incoming requests and the HTTP requests are given to the servlet as they are

QUESTION: What is HttpTunneling?
ANSWER: HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intra-network of an organization is blocked by a firewall and the network is exposed to the outer world only through a specific web server port , that listens for only HTTP requests. To use any other protocol, that by passes the firewall, the protocol is embedded in HTTP and send as HttpRequest.


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.