Part1 - JSP and Servlet Questions
1} What is JSP?
Ans : Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet.
2} What are the life-cycle methods for a jsp?
Ans:
public void jspInit()
It is invoked only once, same as init method of servlet.
public void _jspService(ServletRequest request,ServletResponse)throws ServletException,IOException
It is invoked at each request, same as service() method of servlet.
public void jspDestroy()
It is invoked only once, same as destroy() method of servlet.
3} What are the JSP implicit objects ?
Ans:
JSP provides 9 implicit objects by default. They are as follows:
Object (Its Type)
1) out (JspWriter)
2) request (HttpServletRequest)
3) response (HttpServletResponse)
4) config (ServletConfig)
5) session (HttpSession)
6) application (ServletContext)
7) pageContext (PageContext)
8) page (Object)
9) exception (Throwable)
4} What is servlet?
A: A servlet is a Java prog ramming lang uag e class that is used to extend the capabilities of servers that host applications accessed by means of a request- response prog ramming model. Before the servlet, CGI scripting lang uag e was used as server side prog ramming lang uag e.
5} What is the use of servlet?
Ans: Uses of servlet includes:
Processing and storing data submitted by an HT ML form.
Providing dynamic content.
A Servlet can handle multiple request concurrently and be used to develop hig h performance system
Manag ing state information on top of the stateless HT T P.
6} What is the life c yc le of servlet?
Ans: Life cycle of Servlet:
Servlet class loading
Servlet instantiation
Initialization (call the init method)
Request handling (call the service method)
Removal from service (call the destroy method)
7} Why do we need construc tor in servlet if we use the init ()?
Ans: Even thoug h there is an init method in a servlet which g ets called to initialize it, a constructor is still required to instantiate the servlet. Even thoug h you as the developer would never need to explicitly call the servlet's constructor, it is still being used by the container.
8} How servlet is loaded?
Ans: The servlet is loaded by:
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Administrator can manually loads.
9} When the servlet is unloaded?
Ans: Servlet gets unloaded when:Server shuts down.
Administrator manually unloads.
10} What is servlet interface?
Ans: 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.
11} What is the generic servlet class?
Ans: GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface. In addition to the methods declared in these two interfaces, this class also provides simple versions of the lifecycle methods init() and destroy(), and implements the log method declared in the ServletContext interface.
12} What is the differenc e between Generic Servlet and HttpServlet?
Ans: The difference is:
The GenericServlet is an abstract class that is extended by HttpServlet to provide HT T P protocol- specific methods. But HttpServlet extends the GenericServlet base class and provides a framework for handling the HTTP protocol.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers. The HttpServlet subclass passes g eneric service method requests to the relevant doGet () or doPost () method.
GenericServlet is not specific to any protocol. HttpServlet only supports HT T P and HT T PS protocol.
13} Why HttpServlet class is declared abstract?
Ans: The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. T his is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet () requests for example, there is no need to write a doPost () method too.
14} Can servlet have a constructor?
Ans: Yes
15} What are the type of protocols supported by the HttpServlet?
Ans: It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.
16} What is the differenc e between the doGet () and doPost ()?
Ans: The difference is:
In doGet() the parameters are appended to the URL and sent along with header information. In doPost (),send the information throug h a socket back to the webserver and it won't show up in the URL bar. The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. You can send much more information to the server by using post and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! DoGet() is a request for information.It does not chang e anything on the server. (doGet () should be idempotent). doPost () provides information (such as placing an order for merchandise) that the server is expected to remember.
17} When to use doGet() and when doPost()?
Ans:Always prefer to use GET (As because GET is faster than POST ), except mentioned in the following reason:
If data is sensitive.Data is g reater than 1024 characters.
If your application don't need bookmarks.
18} How do I support both doGet () and doPost () from same servlet?
Ans:The easy way is, just support POST , then have your doGet method call your doPost method.
19} Should I override the service() method?
Ans: We never override the service method, since the HT T P Servlets have already taken care of it. The default service function invokes the doXXX() method corresponding to the method of the HT T P request. For example, if the HTTP request method is GET , doGet () method is called by default. A servlet should override the doXXX() method for the HT T P methods that servlet supports. Because HTTP service method checks the request method and calls the appropriate handler method, it is not necessary to override the service method itself. Only override the appropriate doXXX() method.
20} What is the ServletContext?
Ans: A servlet context object contains the information about the Web application of which the servlet is a part. It also provides access to the resources common to all the servlets in the application. Each Web application in a container has a sing le servlet context associated with it.
21} What is the differenc e between the ServletConfig and ServletContext interface?
Ans: The ServletConfig interface is implemented by the servlet container 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. A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.
22} What is the differenc e between forward () and sendRedirec t ()?
Ans: The difference is:
A forward is performed internally by the servlet. A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the orig inal.
The browser is completely unaware that it has taken place, so its orig inal URL remains intact. But in sendRedirect, the browser, in this case, is doing the work and knows that it's making a new request.
23} What is the differenc e between forward() and inc lude()?
Ans: The RequestDispatcher include() method inserts the contents of the specified resource directly in the flow of the servlet response, as if it were part of the calling servlet. The RequestDispatcher forward() method is used to show a different resource in place of the servlet that was orig inally called.
24} What is the use of servlet wrapper c lasses?
Ans: The HttpServletRequestWrapper and HttpServletResponseWrapper classes are desig ned to make it easy
for developers to create custom implementations of the servlet request and response types.
The classes are constructed with the standard HttpServletRequest and HttpServletResponse instances respectively and their default behaviour is to pass all method calls directly to the underlying objects.
25} What is a deployment descriptor?
Ans: A deployment descriptor is an XML document with an .xml extension. It defines a component's deployment setting s. It declares transaction attributes and security authorization for an enterprise bean. The information provided by a deployment descriptor is declarative and therefore it can be modified without changing the source code of a bean.
26} What is the preinitialization of servlet?
Ans: A container does not initialize the servlets as soon as it starts up; it initializes a servlet when it receives a request for that servlet first time. T his is called lazy loading .
The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.
Ans : Java Server Pages technology (JSP) is used to create dynamic web page. It is an extension to the servlet technology. A JSP page is internally converted into servlet.
2} What are the life-cycle methods for a jsp?
Ans:
public void jspInit()
It is invoked only once, same as init method of servlet.
public void _jspService(ServletRequest request,ServletResponse)throws ServletException,IOException
It is invoked at each request, same as service() method of servlet.
public void jspDestroy()
It is invoked only once, same as destroy() method of servlet.
3} What are the JSP implicit objects ?
Ans:
JSP provides 9 implicit objects by default. They are as follows:
Object (Its Type)
1) out (JspWriter)
2) request (HttpServletRequest)
3) response (HttpServletResponse)
4) config (ServletConfig)
5) session (HttpSession)
6) application (ServletContext)
7) pageContext (PageContext)
8) page (Object)
9) exception (Throwable)
4} What is servlet?
A: A servlet is a Java prog ramming lang uag e class that is used to extend the capabilities of servers that host applications accessed by means of a request- response prog ramming model. Before the servlet, CGI scripting lang uag e was used as server side prog ramming lang uag e.
5} What is the use of servlet?
Ans: Uses of servlet includes:
Processing and storing data submitted by an HT ML form.
Providing dynamic content.
A Servlet can handle multiple request concurrently and be used to develop hig h performance system
Manag ing state information on top of the stateless HT T P.
6} What is the life c yc le of servlet?
Ans: Life cycle of Servlet:
Servlet class loading
Servlet instantiation
Initialization (call the init method)
Request handling (call the service method)
Removal from service (call the destroy method)
7} Why do we need construc tor in servlet if we use the init ()?
Ans: Even thoug h there is an init method in a servlet which g ets called to initialize it, a constructor is still required to instantiate the servlet. Even thoug h you as the developer would never need to explicitly call the servlet's constructor, it is still being used by the container.
8} How servlet is loaded?
Ans: The servlet is loaded by:
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Administrator can manually loads.
9} When the servlet is unloaded?
Ans: Servlet gets unloaded when:Server shuts down.
Administrator manually unloads.
10} What is servlet interface?
Ans: 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.
11} What is the generic servlet class?
Ans: GenericServlet is an abstract class that implements the Servlet interface and the ServletConfig interface. In addition to the methods declared in these two interfaces, this class also provides simple versions of the lifecycle methods init() and destroy(), and implements the log method declared in the ServletContext interface.
12} What is the differenc e between Generic Servlet and HttpServlet?
Ans: The difference is:
The GenericServlet is an abstract class that is extended by HttpServlet to provide HT T P protocol- specific methods. But HttpServlet extends the GenericServlet base class and provides a framework for handling the HTTP protocol.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers. The HttpServlet subclass passes g eneric service method requests to the relevant doGet () or doPost () method.
GenericServlet is not specific to any protocol. HttpServlet only supports HT T P and HT T PS protocol.
13} Why HttpServlet class is declared abstract?
Ans: The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. T his is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet () requests for example, there is no need to write a doPost () method too.
14} Can servlet have a constructor?
Ans: Yes
15} What are the type of protocols supported by the HttpServlet?
Ans: It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.
16} What is the differenc e between the doGet () and doPost ()?
Ans: The difference is:
In doGet() the parameters are appended to the URL and sent along with header information. In doPost (),send the information throug h a socket back to the webserver and it won't show up in the URL bar. The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. You can send much more information to the server by using post and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! DoGet() is a request for information.It does not chang e anything on the server. (doGet () should be idempotent). doPost () provides information (such as placing an order for merchandise) that the server is expected to remember.
17} When to use doGet() and when doPost()?
Ans:Always prefer to use GET (As because GET is faster than POST ), except mentioned in the following reason:
If data is sensitive.Data is g reater than 1024 characters.
If your application don't need bookmarks.
18} How do I support both doGet () and doPost () from same servlet?
Ans:The easy way is, just support POST , then have your doGet method call your doPost method.
19} Should I override the service() method?
Ans: We never override the service method, since the HT T P Servlets have already taken care of it. The default service function invokes the doXXX() method corresponding to the method of the HT T P request. For example, if the HTTP request method is GET , doGet () method is called by default. A servlet should override the doXXX() method for the HT T P methods that servlet supports. Because HTTP service method checks the request method and calls the appropriate handler method, it is not necessary to override the service method itself. Only override the appropriate doXXX() method.
20} What is the ServletContext?
Ans: A servlet context object contains the information about the Web application of which the servlet is a part. It also provides access to the resources common to all the servlets in the application. Each Web application in a container has a sing le servlet context associated with it.
21} What is the differenc e between the ServletConfig and ServletContext interface?
Ans: The ServletConfig interface is implemented by the servlet container 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. A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.
22} What is the differenc e between forward () and sendRedirec t ()?
Ans: The difference is:
A forward is performed internally by the servlet. A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the orig inal.
The browser is completely unaware that it has taken place, so its orig inal URL remains intact. But in sendRedirect, the browser, in this case, is doing the work and knows that it's making a new request.
23} What is the differenc e between forward() and inc lude()?
Ans: The RequestDispatcher include() method inserts the contents of the specified resource directly in the flow of the servlet response, as if it were part of the calling servlet. The RequestDispatcher forward() method is used to show a different resource in place of the servlet that was orig inally called.
24} What is the use of servlet wrapper c lasses?
Ans: The HttpServletRequestWrapper and HttpServletResponseWrapper classes are desig ned to make it easy
for developers to create custom implementations of the servlet request and response types.
The classes are constructed with the standard HttpServletRequest and HttpServletResponse instances respectively and their default behaviour is to pass all method calls directly to the underlying objects.
25} What is a deployment descriptor?
Ans: A deployment descriptor is an XML document with an .xml extension. It defines a component's deployment setting s. It declares transaction attributes and security authorization for an enterprise bean. The information provided by a deployment descriptor is declarative and therefore it can be modified without changing the source code of a bean.
26} What is the preinitialization of servlet?
Ans: A container does not initialize the servlets as soon as it starts up; it initializes a servlet when it receives a request for that servlet first time. T his is called lazy loading .
The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.