G  Tech Lab
About us   |   Contact us   |   Help
Site Search   
G Tech Lab News
G Tech Lab Java Centre
G Tech Lab Provides different interview question and answer, tutorials .
Shortly comming up.... forum for different programming languages.
Read more
INTERVIEW QUESTION

JAVA

Core Java Interview Question
JSP Interview Question
Servlet Interview Question
EJB Interview Question

DATABASE

SQL Interview Question
ORACLE Interview Question
SQL SERVER Interview Question
My SQL Interview Question

1) How do I support both GET and POST protocol from the same Servlet?

The easy way is, just support POST, then have your doGet method call your doPost method:

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); }

2) How do I ensure that my servlet is thread-safe?

  • This is actually a very complex issue. A few guidelines:

    1. The init() method is guaranteed to be called once per servlet instance, when the servlet is loaded. You don't have to worry about thread safety inside this method, since it is only called by a single thread, and the web server will wait until that thread exits before sending any more threads into your service() method.

    2. Every new client request generates (or allocates) a new thread; that thread calls the service() method of your servlet (which may in turn call doPost() , doGet() and so forth).

    3. Under most circumstances, there is only one instance of your servlet, no matter how many client requests are in process. That means that at any given moment, there may be many threads running inside the service() method of your solo instance, all sharing the same instance data and potentially stepping on each other's toes. This means that you should be careful to synchronize access to shared data (instance variables) using the synchronized keyword.

    (Note that the server will also allocate a new instance if you register the servlet with a new name and, e.g., new init parameters.)

    4. Note that you need not (and should not) synchronize on local data or parameters. And especially you shouldn't synchronize the service() method! (Or doPost() , doGet() et al .)

    5. A simple solution to synchronizing is to always synchronize on the servlet instance itself using " synchronized (this) { ... } ". However, this can lead to performance bottlenecks; you're usually better off synchronizing on the data objects themselves.

    6. If you absolutely can't deal with synchronizing, you can declare that your servlet " implements SingleThreadModel ". This empty interface tells the web server to only send one client request at a time into your servlet. From the JavaDoc: "If the target servlet is flagged with this interface, the servlet programmer is guaranteed that no two threads will execute concurrently the service method of that servlet. This guarantee is ensured by maintaining a pool of servlet instances for each such servlet, and dispatching each service call to a free servlet. In essence, if the servlet implements this interface, the servlet will be thread safe." Note that this is not an ideal solution, since performance may suffer (depending on the size of the instance pool), plus it's more difficult to share data across instances than within a single instance.

3) What is the difference between URL encoding, URL rewriting, HTML escaping, and entity encoding?

URL Encoding is a process of transforming user input to a CGI form so it is fit for travel across the network -- basically, stripping spaces and punctuation and replacing with escape characters. URL Decoding is the reverse process. To perform these operations, call java.net.URLEncoder.encode() and java.net.URLDecoder.decode() (the latter was (finally!) added to JDK 1.2, aka Java 2).

Example: changing " We're #1! " into " We%27re+%231%21 "

URL Rewriting is a technique for saving state information on the user's browser between page hits. It's sort of like cookies, only the information gets stored inside the URL, as an additional parameter. The HttpSession API, which is part of the Servlet API, sometimes uses URL Rewriting when cookies are unavailable.

Example: changing <A HREF="nextpage.html"> into
<A HREF="nextpage.html;$sessionid$=DSJFSDKFSLDFEEKOE"> (or whatever the actual syntax is; I forget offhand)

(Unfortunately, the method in the Servlet API for doing URL rewriting for session management is called encodeURL(). Sigh...)

4) How does a servlet communicate with a JSP page?
The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.

public void doPost (HttpServletRequest request, HttpServletResponse response) { try { govi.FormBean f = new govi.FormBean(); String id = request.getParameter("id"); f.setName(request.getParameter("name")); f.setAddr(request.getParameter("addr")); f.setAge(request.getParameter("age")); //use the id to compute //additional bean properties like info //maybe perform a db query, etc. // . . . f.setPersonalizationInfo(info); request.setAttribute("fBean",f); getServletConfig().getServletContext().getRequestDispatcher ("/jsp/Bean1.jsp").forward(request, response); } catch (Exception ex) { . . . } }

5) How do I call one servlet from another servlet?

use a RequestDispatcher

· use a URLConnection or HTTPClient

· send a redirect

· call getServletContext().getServlet(name) (deprecated, doesn't work in 2.1+)

6) Explain the life cycle methods of a Servlet ?

The javax.servlet.Servlet interface defines the three methods known as life-cycle method.
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()
First the servlet is constructed, then initialized wih the init() method.
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet.

The servlet is removed from service, destroyed with the destroy() methid, then garbaged collected and finalized.

7) Explain the directory structure of a web application.

The directory structure of a web application consists of two parts.
A private directory called WEB-INF
A public resource directory which contains public resource folder.

WEB-INF folder consists of
1. web.xml
2. classes directory
3. lib directory

8)What are the common mechanisms used for session tracking?

Cookies
SSL sessions
URL- rewriting

9)What is ServletContext ?
ServletContext interface is a window for a servlet to view it's environment. A servlet can use this interface to get information such as initialization parameters for the web applicationor servlet container's version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.


10)What is the difference between HttpServlet and GenericServlet?


A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).
Both these classes are abstract.

Page 1


Download
Netbeans IDE 5.5.1. Download
J2SE SDK Download J2RE Download Application Server Download Latest JDK Download JCreator Download JDevloper Download