General Java Servlet questions

  1. What is the servlet?
  2. What are the JSP atrributes?
  3. What is the need of super.init(config) in servlets?
  4. How to know whether we have to use jsp or servlet in our project?
  5. Can we call destroy() method on servlets from service method?
  6. What is the Servlet Interface?
  7. What is the difference between GenericServlet and HttpServlet?
  8. How we can check in particular page the session will be alive or not?
  9. What is the importance of deployment descriptor in servlet?
  10. When we increase the buffer size in our project using page directive attribute ‘buffer’ what changes we observe?
  11. What is the difference between ServetConfig and ServletContext..?
  12. When a servlet accepts a call from a client, it receives two objects. What are they?
  13. What are the differences between GET and POST service methods?
  14. In which conditions we have to use the ServletContext?
  15. What methods will be called in which order?((i.e)service(),doget(),dopost())
  16. Servlet is Java class. Then why there is no constructor in Servlet? Can we write the constructor in Servlet
  17. What is the use of ServletConfig and ServletContext..?
  18. What information that the ServletRequest interface allows the servlet access to?
  19. What is the difference between ServletContext and ServletConfig?
  20. When do you have action=get?
  21. What is a Singleton class. How do you write it?
  22. What is difference between sendRedirect() and forward()..? Which one is faster then other and which works on server?
  23. What information that the ServletResponse interface gives the servlet methods for replying to the client?
  24. Can I invoke a JSP error page from a servlet?
  25. Can a init(ServletConfig config) method be overrided in servlets?
  26. How many ServletConfig and servlet context objects are present in one application?
  27. Do we have a constructor in servlet? can we explictly provide a constructor in servlet programme as in java program?
  28. What are the uses of Servlets?
  29. Can I just abort processing a JSP?
  30. Is servlet is used to create a dynamic webpage or Static webpage or both?
  31. If you want a servlet to take the same action for both GET and POST request, what should you do?
  32. What is the difference between JSP and SERVLETS?
  33. What are the advantages using servlets than using CGI?
  34. What is a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or synchronization?
  35. We have two applications in that we have two servlets each.How they(servlets) communicate with each other?
  36. How the server will know (i.e) when it can invoke init, service,destroy methods of servlet life cycle?
  37. How to communicate between two servlets?
  38. What is the difference between servlets and applets?
  39. How will u pass the argument from one servlet to another servlet?
  40. What method used to add a jsp to the servlet?
  41. How HTTP Servlet handles client requests?
  42. How to get one Servlet’s Context Information in another Servlet?
  43. Difference between single thread and multi thread model servlet
  44. What is the super class of All servlets?
  45. How are Servlet Applet communication achieved?
  46. What is servlet context and what it takes actually as parameters?
  47. What is the servlet life cycle?
  48. Types of Servlets?
  49. Why is that we deploy servlets in a webserver.What exactly is a webserver?
  50. Which code line must be set before any of the lines that use the PrintWriter?
This entry was posted in Java, Networking. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

39 Comments on General Java Servlet questions

  1. Viswanathan Ramasamy
    Posted 11/23/2005 at 2:18 am | Permalink

    Servlets are Java technology’s answer to Common Gateway Interface (CGI)
    programming. They are programs that run on a Web server, acting as a middle
    layer between a request coming from a Web browser or other HTTP client
    and databases or applications on the HTTP server.Their job is to:

    1. Read any data sent by the user.
    2. Look up any other information about the request that is
    embedded in the HTTP request.
    3. Generate the results.
    4. Format the results inside a document.
    5. Set the appropriate HTTP response parameters.
    6. Send the document back to the client.

    for omer information on this refer : http://pdf.coreservlets.com/CSAJSP-Chapter1.pdf

  2. Manish
    Posted 4/26/2006 at 7:56 am | Permalink

    Servlet is a java interface for distributed n-tire programming. It process request of HttpClient and send result through response by residing in Web server.

  3. Manish
    Posted 4/26/2006 at 7:57 am | Permalink

    Super.init(config) in servlet - to get servelt context.

  4. Manish
    Posted 4/26/2006 at 7:59 am | Permalink

    Q5 >> In Httpsevletimplementation, i mean in doGet,doPost we can not call destory method of service.

  5. Manish
    Posted 4/26/2006 at 7:59 am | Permalink

    Servlet interface is a java interface to implement sevice methods

  6. Manish
    Posted 4/26/2006 at 8:02 am | Permalink

    Httpservlet implements Generic servlet and has additional features regrading to http.

  7. Rashmi Babtiwale
    Posted 7/3/2006 at 6:59 am | Permalink

    I need a little help with session tracking in servlets. Can anyone tell me in brief as to what all methods are used for session tracking?

  8. Sahil
    Posted 8/13/2006 at 10:54 am | Permalink

    In Servlet i am using sendRedirect(), to redirect to some other url, but i want that the page from which i am redirecting should wait for some specified time and then it redirect to the specific page. Is there any method available?

    Note: Like in 10 seconds ur directed to http://www.yahoo.com

  9. Sahil Pandey
    Posted 8/13/2006 at 10:59 am | Permalink

    Can we call destroy() method on servlets from service method?

    Yes, we can call the destroy() in the service() of the servlet but it won’t destroy the existing objects,and it won’t does the natural task that container can do when it calls destroy().

  10. Sahil Pandey
    Posted 8/13/2006 at 11:05 am | Permalink

    Q.How we can check in particular page the session will be alive or not?
    Ans: By calling isAlive().

  11. Sahil Pandey
    Posted 8/13/2006 at 11:11 am | Permalink

    Q.What are the differences between GET and POST service methods?
    Ans:

    GET() is default method while POST() is not.
    GET() is insecure(Data get disposed)ex.google. but POST is secure.
    GET() is limited to data, we cant sent long stream of data,limited only to 2K, while this is not with POST().
    All popular search engines uses GET().
    GET() is used for sending insecure data while POST() is for secure data.

    Note: Also check for doDelete(),doPut() etc.

  12. Sahil Pandey
    Posted 8/13/2006 at 11:18 am | Permalink

    Q.What is difference between sendRedirect() and forward()..? Which one is faster then other and which works on server?

    Ans:
    By using sendRedirect() one can redirect to a particular url in a existing web application and can out side url.

    eg.

    sendRedirect(”urlpattern”);
    and
    sendRedirect(”www.yahoo.com”);

    Note: in this the WEB BROWSER is the main working horse. in this the client had to do(web Browser)

    In forward() we can only forward the particular url pattern available inside the web aaplication. In this web container is the main working horse.

    eg.
    forward(”urlpattern”);

    This work on the server

    About the speed i am not sure. According to me forward() is faster

  13. Sahil Pandey
    Posted 8/13/2006 at 11:24 am | Permalink

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

    Ans:

    About CGI

    Pearl is used to write CGI prog. that is not platform dependent.

    separate address space for every request..

    no thread concept is there in Pearl.

    Servlet.

    Written in java that is Platform Independent.
    same object is shared for every request.
    Threads run in behind to track all the request

  14. Sahil Pandey
    Posted 8/13/2006 at 11:27 am | Permalink

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

    Ans:

    PrintWriter pw=res.getWriter();

  15. vijayakumar .D.R.
    Posted 8/18/2006 at 7:08 am | Permalink

    what is servlet interface ?

    Defines methods that all servlets must implement.

    A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.

    To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP servlet that extends javax.servlet.http.HttpServlet.

    This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:

    The servlet is constructed, then initialized with the init method.
    Any calls from clients to the service method are handled.
    The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
    In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup information, and the getServletInfo method, which allows the servlet to return basic information about itself, such as author, version, and copyright.

  16. Gaurav
    Posted 9/4/2006 at 1:27 am | Permalink

    Generic servlet responds to all protocols but http servlets responds only to http protocol.

  17. nowshad
    Posted 11/13/2006 at 3:27 am | Permalink

    Q.What is the difference between ServetConfig and ServletContext..?
    Ans.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.

  18. atul agarwal
    Posted 1/17/2007 at 10:30 am | Permalink

    Super.init(config) in servlet —-when u choose to override this method you should call this method means without calling getServletCofig or getInitParameter we can get a cofiguration data.

  19. Sandeep
    Posted 2/3/2007 at 10:09 am | Permalink

    The Java Servlet API allows a software developer to add dynamic content to a Web server using the Java platform

    A Servlet is an object that receives a request (ServletRequest) and generates a response (ServletResponse) based on the request

  20. Naga,
    Posted 2/15/2007 at 10:11 am | Permalink

    What is the servlet life cycle?
    ans>
    init(..)–>service(..)–>destroy().

    1)web container calls the init(SercletConfig config)
    method whenever the Servlet Object is Created.
    2)service(..) method is called whenever the client made the request
    3) destroy() method is called whenever the webcontainer is going to remove the servlet Object …………..

  21. Vikram
    Posted 2/19/2007 at 4:14 pm | Permalink

    What is the servlet?
    servlet is a server side program.It is similar to CGI but differs in persistance.The life of CGI is till it serves a task, varus once the servlet is started it persistant and can serve multiple request.

  22. Vikram
    Posted 2/19/2007 at 4:27 pm | Permalink

    7.What is the difference between GenericServlet and HttpServlet?

    Http servlet extends javax.servlet.GenericServlet an abstract class that implements most of the basic servlet methog.

    Generic servlet on the other hand implements the Servlet interface.

  23. Gayathri
    Posted 2/20/2007 at 3:19 pm | Permalink

    When we increase the buffer size in our project using page directive attribute ‘buffer’ what changes we observe?

    We cannot use the and action as the buffer is flushed before the target page is invoked.

  24. Gayathri
    Posted 2/20/2007 at 3:20 pm | Permalink

    jsp:include and jsp:forward

  25. Kumarvijay
    Posted 4/9/2007 at 10:48 pm | Permalink

    For session tracking as i know there are 4 types one is using 1)cookie 2)URL Rewriting 3)Authentication 4)Hidden Field . As browser can disable cookies we can go for hidden field but the problem with hidden field is u can use it only in form . So we go for Authentication but this is not good practice Best method is URL rewriting as it will work even cookies are disable in browser..
    If u need more about this drop me a mail..At Kumarvijay.walikar@gmail.com

  26. Kumarvijay
    Posted 4/9/2007 at 10:58 pm | Permalink

    Why Servlet Better than CGI?
    In CGI for every client request a new process object is created But in Servlet only one object is created whenever the first request comes to that servelt. And after that for every new request a thread is launched and
    all thread share the common object so its scalable.
    And its platform independent.

  27. sridhar
    Posted 4/27/2007 at 6:45 am | Permalink

    javax.http.HttpServlet class has no abstract methods but it is abstract class (i know that abstract classes no need to have atleast one abstract method) ..what is the intention in that

  28. suresh
    Posted 5/18/2007 at 6:32 am | Permalink

    Can we call destroy() method on servlets from service method?

  29. Suman
    Posted 6/22/2007 at 9:00 am | Permalink


    7. What is the difference between GenericServlet and HttpServlet?

    Ans: the Generic Servlet was used before 1990 and is of primitive nature whereas HTTPServlet has come in 1990 with added features to GenericServlet. Although the SuperClass to HTTPServlet is : javax.servlet.Servlets.GenericServlets.
    18. What information that the ServletRequest interface allows the servlet access to?

    Ans: The ServletRequest interface allows the the Client to send the request to the server for accessing the required response through the Servlet Response which is received by the Client from the Server.

    44. What is the super class of All servlets?

    Ans: the super class of all servlets is : - javax.servlet.Servlets

    48. Types of Servlets?
    Ans: There are 2 types of servlets a. Generic servlet
    b. HTTP Servlet.

    49. Why is that we deploy servlets in a webserver.What exactly is a webserver?

    Ans.: If we don't deploy the servlet in the webserver then it will be difficult to compile the servlet using the weblogic/tomcat etc..We deploy the servlet in the Web.xml file. A web Server is the Server which basically helps in communicating between the Client and the Serverin order to get the Request and send the response using alias for security purpose.

  30. Rafi
    Posted 6/23/2007 at 12:37 pm | Permalink

    Q.What is the difference between JSP and SERVLETS?

    Ans:
    1.For jsp java code is used in html tag.
    For servlet html tag is used in java code.
    2.It is very difficult to write so many println()
    in a sevlet to generate a html page.
    But in case of jsp no need of that.

  31. Mike Mountrakis
    Posted 7/8/2007 at 6:16 am | Permalink

    1. What is the servlet?
    A Servlet is a class that prototypes objects living within the Application Server or Plain HTTP Server.
    They are the implementation part of the application,
    while the Application Server or HTTP Server ensures the integrity of the HTTP communication.

    Normally, Servlets extend HttpServlet class and
    implement one (or maybe both) of the following two methods
    public void doGet()
    public void doPost()
    And for sure initialization method:
    public void init(ServletConfig config)

    2. What are the JSP atrributes?
    JSP attributes are the attributes that are used by JSP Components,
    For example can be the length of a text field or the foreground color of a label.

    3. What is the need of super.init(config) in servlets?
    Initialize attributes of the parent class for example the class reference count.
    Normally the developer implements the init() method adding initialization code for his
    own child - class attributes and the first row in the implementation must be the call to the superclass init():
    public void init(ServletConfig config)
    {
    super.init(config);
    /*
    Your initialization code fits here…
    */

    }

    4. How to know whether we have to use jsp or servlet in our project?
    Depends what we have to design. For example an application that needs to have a brawser based GUI has
    no other JAVA options than using JSP. On the other hand, a servlet can interact with a siple POST/GET
    event without any other forms.

    5. Can we call destroy() method on servlets from service method?
    Calling destroy() results to destroy the brawser’s session with the Servlet instance.
    It will not affect the servlet container, meaning that if one client reaches a code point calling destroy(),
    the servlet will remain persistent for all other sessions.

    6. What is the Servlet Interface?
    A Set of Methods that servlets must implement.

    7. What is the difference between GenericServlet and HttpServlet?
    In GenericServlet you have to implement the Presentation Layer methods if for example you wish to use XML.
    HttpServelt is a Servlet subclass implementing Http protocol.

    8. How we can check in particular page the session will be alive or not?
    Use isAlive() method

    9. What is the importance of deployment descriptor in servlet?

    10. When we increase the buffer size in our project using page directive attribute ‘buffer’ what changes we observe?
    Ability to receive and transmit over HTTP.

    12. When a servlet accepts a call from a client, it receives two objects. What are they?

    ServletRequest request, ServletResponse response for a GenericServlet originated class.
    HttpServletRequest request, HttpServletResponse response for HttpServlet originated class

    The client brawser’s request and response.

    15. What methods will be called in which order?((i.e)service(),doget(),dopost())
    init() -> doGet() -> destroy()

    16. Servlet is Java class. Then why there is no constructor in Servlet? Can we write the constructor in Servlet
    init() is called instead by the HTTP server.

    Michael Mountrakis
    http://www.illumine.gr

  32. Rohan Ambasta
    Posted 7/28/2007 at 2:46 am | Permalink

    Why the GenericServlet class is abstract ?

    object cannot be created for an Interface . But the webcontainer provides an object for the interface.comment !

  33. saurabh kushwaha
    Posted 9/20/2007 at 6:37 am | Permalink

    What is a Singleton class. How do you write it?

    A class whch cn have only one instance.
    make the construtor of the class private
    add a staic method that return object type of that class.

    code :

    class SingletonClass
    {
    SingletonClass singletonclass = null;
    private SingletonClass()
    {
    }
    public static SingletonClass getInstance()
    {
    singletonclass = new SingletonClass();
    return singletonclass;
    }
    }

  34. sameer
    Posted 11/13/2007 at 5:07 am | Permalink

    What is servlet?
    servlet is a multi threading program and it is a middle ware b/w the client request to the client response.

  35. praveenoruganti
    Posted 11/14/2007 at 6:50 am | Permalink

    Advantages of Servlets:

    With so many choices for servers-side software development, one can wonder why Java servlet technology is superior to others. Server-side Java technologies give us platform independence, efficiency, access to other enterprise Java APIs, reusability, and modularity.

    As Java servlets enjoy the same benefits of a regular Java program, they are platform-independent and can be ported across various platforms that support Java.

    As compared with predecessors of Java servlet technology, Java servlets performs quite exceedingly well. Due to its distinct and unique multi-threading capability, it is possible for hundreds and even thousands of users can access the same servlet simultaneously without affecting the load of the web server.

    Since servlets are an extension of the Java platform, they can access all of the Java APIs. A Java servlet can send and receive email, invoke methods of objects using Java RMI or CORBA, object directory information using the JNDI package, make use of an Enterprise JavaBean (EJB), or may other part of the ever-growing platform.

    Software reusability is the essential mantra for software development. As servlets are web components, they can be reused easily. Also Java, as an OO language, helps to encapsulate shared functionality to be reused. Thus Java servlets are reusable.

  36. rohit
    Posted 11/27/2007 at 12:32 am | Permalink

    When you want a servlet to take the same action for both GET and POST request, what should you do?

    The service method should be used to perform the task. there are two ways to implement this.
    1 call service method from doGET and doPOST methods by passing the response and request parameters.

    2 code only the service method..

  37. siri
    Posted 12/28/2007 at 3:21 pm | Permalink

    Difference between single thread and multi thread model servlet

    Ans) Typically, a servlet class is instantiated the first time it is invoked. The same instance will be used over several client requests, so all members that are declared in that servlet are shared accross clients. That is what is meant by multi threaded model, multiple clients that access the same 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.

  38. siri
    Posted 12/28/2007 at 3:33 pm | Permalink

    Is servlet is used to create a dynamic webpage or Static webpage or both?

    You can do any thing. But server side programming is designed to generate dynamic web pages/designs. If static pages are needed, make it simple with static HTMLmpages.

  39. siri
    Posted 12/28/2007 at 3:46 pm | Permalink

    Why is that we deploy servlets in a webserver.What exactly is a webserver

    ans) Web server is nothing but program written in Java etc.Ex:Tomcat 5.0

    The webserver is exactly running in the JVM.so,whataver the program is written for Tomcat is running in JVM and displaying.

    We deploy the class file of the served, & servlet needed to execute run time environment & web server provide the run time environment for executing the servlet, each server has the own JVM

    A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send a redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser. Understand that a Web server’s delegation model is fairly simple. When a request comes into the Web server, the Web server simply passes the request to the program best able to handle it. The Web server doesn’t provide any functionality beyond simply providing an environment in which the server-side program can execute and pass back the generated responses

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*