Ads 468x60px

Pages

Thursday, 28 June 2012

servlets interview questions


17) What is a Scriptlet?
A scriptlet can contain any number of language statements, variable or expressions that are valid in the page scripting language. Within scriptlet tags, you can declare variables to use later in the file, write expressions valid in the page scripting language, use any of the JSP implicit objects or any object declared with a <jsp:useBean>.  Generally a scriptlet can contain any java code that are valid inside a normal java method. This will become the part of generated servlet's service method.








18) What's the difference between forward and sendRedirect?
forward is server side redirect and sendRedirect is client side redirect. When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container And then returns to the calling method. When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. Client can disable sendRedirect

19) Is JSP extensible ?
Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

20) What is the life cycle of servlet?
Each servlet has the same life cycle: first, the server loads and initializes the servlet by calling the init method. This init() method will be executed only once during the life time of a servlet. Then when a client makes a request, it executes the service method. finally it executes the destroy() method when server removes the servlet.

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

22) What is the need of super.init (config) in servlets ?
Then only we will be able to access the ServletConfig from our servlet. If there is no ServletConfig our servlet will not have any servlet nature

23) What is the difference between GenericServlet and HttpServlet?
GenericServlet supports any protocol. HttpServlet supports only HTTP protocol. By extending GenericServlet we can write a servlet that supports our own custom protocol or any other protocol.

24) Can we write a constructor for servlet ?
Yes. But the container will always call the default constructor only. If default constructor is not present , the container will throw an exception.

25) What is the difference between <%@ include ...> (directive include) and <jsp:include> ?
@ include is static include. It is inline inclusion. The contents of the file will get included on Translation phase. It is something like inline inclusion. We cannot have a dynamic filename for directive include. <jsp:include> is dynamic include. Here the included file will be processed as a separate file and the response will be included. We can have a dynamic filename for <jsp:include>. We can aslo pass parameters to <jsp:include

26) Can I just abort processing a JSP?
Yes. You can put a return statement to abort JSP processing

27) How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
By setting appropriate HTTP header attributes we can prevent caching by the browser

<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>

28) How to refer the "this" variable within a JSP page?
Under JSP 1.0, the page implicit object page is equivalent to "this", and returns a reference to the servlet generated by the JSP page.

29) How many JSP scripting elements and what are they?
There are three scripting elements in JSP . They are declarations, scriptlets, expressions.

30) Can we implement an interface in JSP ?
No

31) What is the difference between ServletContext and PageContext?
ServletContext gives the information about the container and PageContext gives the information about the Request

32) How do you pass data (including JavaBeans) to a JSP from a servlet?
By forwarding the request to the servlet ( the data must be there in the request scope) we can pass the data from a JSP to servlet. Also we can use a session to pass the data.

33) How to pass information from JSP to included JSP?
By using <jsp:param> tag
Servlet Life Cycle Methods
The following are the life cycle methods of a servlet instance:
  • init()
  • service()
  • destroy()
We will look into the each method in detail.


init()

This method is called once for a servlet instance. When first time servlet is called, servlet container creates instance of that servlet and loaded into the memory. Future requests will be served by the same instance without creating the new instance. Servlet by default multithreaded application.init() method is used for inilializing servlet variables which are required to be passed from the deployment descriptor web.xml. ServletConfig is passed as the parameter to init() method which stores all the values configured in the web.xml. It is more convenient way to initialize the servlet.


service()

This method is called for the each request. This is the entry point for the every servlet request and here we have to write our businesslogic or any other processes. This method takes HttpServletRequest and HttpServletresponse as the parameters. It is not mandatory to write this method, normally developers are interested in writing doGet() or doPost() methods which is by default called from the service() method. If you override service(), it is your reponsibility to call the appropriate methods. If you are not overridden the service() method, based on the types of the request the methods will be called.


destroy()

This method will be called once for a instance. It is used for releasing any resources used by the servlet instance. Most of the times it could be database connections, Fill IO operations, etc. destroy() is called by the container when it is removing the instance from the servlet container. Servlet instance is deleted or garbage collected by the container only when the web server issues shut down or the instance is not used for a long time.

Typical uses for HTTP Servlets include:
  • Processing and/or storing data submitted by an HTML form.
  • Providing dynamic content, e.g. returning the results of a database query to the client.
  • Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.
Servlets have several advantages over CGI:
  • A Servlet does not run in a separate process. This removes the overhead of creating a new process for each request.
  • A Servlet stays in memory between requests. A CGI program (and probably also an extensive runtime system or interpreter) needs to be loaded and started for each CGI request.
  • There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
  • A Servlet can be run by a Servlet Engine in a restrictive Sandbox (just like an Applet runs in a Web Browser's Sandbox) which allows secure use of untrusted and potentially harmful Servlets.

0 comments:

Post a Comment