SERVLET_Q

download SERVLET_Q

of 4

Transcript of SERVLET_Q

  • 8/9/2019 SERVLET_Q

    1/4

    Q--What is the servlet?

    A--Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet may be responsible for taking data in an HTML order-entry form and applying the business logic used to update acompany's order database.

    Q--What is the servlet life cycle?

    A--Each servlet has the same life cycle:A server loads and initializes the servlet (init())The servlet handles zero or more client requests (service())The server removes the servlet (destroy()) (some servers do this step only whenthey shut down).

    Q--What is the difference between ServletContext and ServletConfig?

    A--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 towrite events to a log file.

    Q--What are the uses of Servlets?

    A--A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be us

    ed to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task.

    Q--Can I just abort processing a JSP?

    A--Yes. Because your JSP is just a servlet method, you can just put (whereever necessary) a < % return; % >

    Q--Can I invoke a JSP error page from a servlet?

    A_-Yes, you can invoke the JSP error page and pass the exception object to it fr

    om within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets.

    If your servlet opens an OutputStream or PrintWriter, the JSP engine will throwthe following translation error:

    java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained

    The following code snippet demonstrates the invocation of a JSP error page fromwithin a controller servlet:

    protected void sendErrorRedirect(HttpServletRequest request,HttpServletResponse response, String errorPageURL, Throwable e) throws

  • 8/9/2019 SERVLET_Q

    2/4

    ServletException, IOException {request.setAttribute ("javax.servlet.jsp.jspException", e);getServletConfig().getServletContext().getRequestDispatcher(errorPageURL).forward(request, response);}

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    {try {// do something} catch (Exception ex) {try {sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);} catch (Exception e) {e.printStackTrace();}}}

    Q--Difference between single thread and multi thread model serv

    A--Typically, a servlet class is instantiated the first time it is invoked. Thesame instance will be used over several client requests, so all members that aredeclared in that servlet are shared accross clients.

    That is what is meant by multi threaded model, multiple clients that access thesame instance. There are situations where you want to protect your servlet member variables from being modified by different clients.

    In this case, you can have your servlet implement the marker interface SingleThreadModel. Every time a client makes a request to a servlet that implements this

    interface, the engine will create a new instance of the servlet.

    For performance reasons, the engine can also maintain a instance pool, handing out instances as they are needed. Or it could also serialize client requests, executing one after another.

    Q--What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?

    A--The getRequestDispatcher(String path) method of javax.servlet.ServletRequestinterface accepts parameter the path to the resource to be included or forwardedto, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root.

    The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root.

    Q--What is the Servlet Interface?

    A--The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a

    class that implements it such as HttpServlet.Servlets-->Generic Servlet-->HttpServlet-->MyServlet.The Servlet interface declares, but does not implement, methods that manage the

  • 8/9/2019 SERVLET_Q

    3/4

    servlet and its communications with clients. Servlet writers provide some or allof these methods when developing a servlet.

    Q-- Question: If you want a servlet to take the same action for both GET and POST request, what should you do?

    A--Simply have doGet call doPost, or vice versa.

    Q--Which code line must be set before any of the lines that use the PrintWriter?

    A--setContentType() method must be set before transmitting the actual document.

    Q--What are the advantages using servlets than using CGI?

    A--Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. It is efficient, convenient, powerful, portable, secureand inexpensive.

    Servlets also address the problem of doing server-side programming with platform-specific APIs. They are developed with Java Servlet API, a standard Java extension.

    Q--What is the difference between servlets and applets?

    A--Servlets are to servers. Applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

    Q--When a servlet accepts a call from a client, it receives two objects. What ar

    e they?

    A--ServeltRequest: which encapsulates the communication from the client to the server.ServletResponse: which encapsulates the communication from the servlet back to the client.ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.

    Q--What information that the ServletResponse interface gives the servlet methodsfor replying to the client?

    A--It Allows the servlet to set the content length and MIME type of the reply. Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data.

    Q--What information that the ServletRequest interface allows the servlet accessto?

    A--Information such as the names of the parameters passed in by the client, theprotocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods

    Q--What is the difference between GenericServlet and HttpServlet?

  • 8/9/2019 SERVLET_Q

    4/4

    A--GenericServlet is for servlets that might not use HTTP, like for instance FTPservice.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 aregiven to the servlet as they are.

    Q--How HTTP Servlet handles client requests?

    A--An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request toa method designed to handle that request.

    Q--What is a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?

    A--Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increas

    e in the future, you may be better off implementing explicit synchronization foryour shared data.

    The key however, is to effectively minimize the amount of code that is synchronzied sothat you take maximum advantage of multithreading. Also, note that SingleThreadModel is pretty resource intensive from the server's perspective.

    The most serious issue however is when the number of concurrent requests exhaustthe servlet instance pool. In that case, all the unserviced requests are queueduntil something becomes free - which results in poor performance. Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool.

    Q--What are the differences between GET and POST service methods?

    A--A GET request is a request to get a resource from the server. Choosing GET asthe "method" will append all of the data to the URL and it will show up in theURL bar of your browser. The amount of information you can send back using a GETis restricted as URLs can only be 1024 characters. A POST request is a requestto post (to send) form data to a resource on the server.

    A POST on the other hand will (typically) send the information through a socketback 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 Javaobjects!