June 07, 2011

Servlets™ and JSP™ Technologies FAQ

What are Servlets and how can they help in developing Web Applications?
"Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order database. Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.Servlets can be embedded in many different servers because the servlet API, which you use to write servlet assumes nothing about the server's environment or protocol. Servlets have become most widely used within HTTP servers; many web servers support the Servlet API. 


What is the Advantage of using Servlets over CGI programming?
Servlets are only instantiated when the client first accesses the program. That is, the first time any client hits the URL that is used to access the Servlet, the Servlet engine instantiates that object. All subsequent accesses are done to that instance. This keeps the response time of Servlets lower than that of CGI programs, which must be run once per hit. Also, because a Servlet is instantiated only once, all accesses are put through that one object. This helps in maintaining objects like internal Connection Pooling or user session tracking and lots of other features.
Here are a few more of the many applications for Servlets:
1. Allowing collaboration between people. A Servlet can handle multiple requests concurrently, and can synchronize requests. This allows Servlets to support systems such as on-line conferencing.
2. Forwarding requests. Servlets can forward requests to other servers and Servlets. Thus Servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.


What is the Jakarta Project ?
The goal of the Jakarta Project is to provide commercial-quality server solutions based on the Java Platform that are developed in an open and cooperative fashion. The flagship product, Tomcat, is a world-class implementation of the Java Servlet 2.2 and JavaServer Pages 1.1Specifications. This implementation will be used in the Apache Web Server as well as in other web servers and development tools." (The Apache Software Foundation). 


Where can i find a good tutorial and more details on Servlets?
These URL's are a good starting point if you want to know more about Servlets.
<!--[if !supportLists]-->§  <!--[endif]-->http://java.sun.com/products/servlet/
<!--[if !supportLists]-->§  <!--[endif]-->http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html
<!--[if !supportLists]-->§  <!--[endif]-->Developing Applications using Servlets (Tutorial)


How do I connect to a database from my Servlet?
Java includes support for databases, using Java Database Connectivity (JDBC). Most modern databases offer JDBC drivers, which allow you to connect any Java application (including Servlets) directly to your database.
If your database vendor does not offer a JDBC driver, and there are no third party drivers available, you may like to use a JDBC bridge. For example, the ODBC-JDBC bridge allows you to connect to any ODBC data source, which many databases support. Also there are four type of Drivers 
Type I   :   JDBC-ODBC bridge
Type II  :   native-API partly JavaTM technology-enabled driver
Type III :   net-protocol fully Java technology-enabled driver
Type IV :   native-protocol fully Java technology-enabled driver
For a list of currently available Database drivers, please visit this page http://industry.java.sun.com/products/jdbc/drivers
Creating a new Connection to Database frequently could slow down your application. Most of the projects use a concept called Connection Pooling. Here when the Servlet starts up, in the init method , a pool of connections are created and are stored in memory (Mostly in a Vector). When a transaction with Database is required, then the next free available connection is retrieved from this Vector and used for that purpose. Once it's work is done, it is again put back into Connections Pool, indicating it is available. Today most of the JDBC Drivers support this feature have inbuilt connection pooling mechanisms.
Please Note : If you are creating your own Connection Pooling, it is strongly recommended to close all the open connections in the destroy method. Else there are chances of your data getting corrupted.


How do I get authentication with myServlet?
Many popular Servlet engines offer Servlet authentication and the API has a call HttpServletRequest.getUserName() which is responsible for returning the username of an authenticated user.


What is a Session Object ? 
Session Object is used to maintain a user session  related information on the Server side. You can store , retrieve and remove information from a Session object according to your program logic. A session object created for each user persists on the server side, either until user closes the browser or user remains idle for the session expiration time, which is configurable on each server.


How to create Session object and use it for storing information ?
This code will get the Session context for the current user and place values of count1 and count2 into MyIdentifier1 and MyIdentifier2 respectively 
HttpSession session = req.getSession(true); //Creating a Session instance
session.putValue ("MyIdentifier1",count1);  // Storing Value into session Object
session.putValue ("MyIdentifier2", count2);  
session.getValue(MyIdentifier1); // Prints value of Count
session.removeValue(MyIdentifier1); // Removing Valuefrom Session Object


What is the Max amount of information that canbe saved in a Session Object ?
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. 


How to confirm that user's browser accepted the Cookie ? 
There's no direct API to directly verify that user's browser accepted the cookie. But the quick alternative would be, after sending the required data tothe users browser, redirect the response to a different Servlet which would try to read back the cookie. If this Servlet is able to read back the cookie, then it was successfully saved, else user had disabled the option to accept cookies. 



No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts