18. How to overwrite the init and destroy method in a jsp page

You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:<%!
public void jspInit() {
. . .
public void jspDestroy() {
. . .

17. What are Servlet and JSP lifecycle methods.

servlet life cycle methods:




jsp lifecycle methods:




remember starting any method with _ means we cannot override this method.

16.What is the difference between sendRedirect()and forward()in JSP?

In an HttpServletResponse class:

Response.sendRedirect () This function is used, when we want to redirect
the client request to some other site (i.e out of our context) or
when ever we want to redirect errors. If you are using sendRedirect (), then it will be visible to the client that means the URL which you have been redirected will be visible in the address bar. Redirect response to the client using the specified redirect location URL.

RequestDispatcher Interface:
Forward() : This can be done in two ways by Request & ServeletContext.
Forwarding a request from a servlet to another resource (servlet, JSP
file, or HTML file) on the server. Forward is done at server side, without the client’s knowledge.

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. When a sendRedirtect 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.

15. What is the difference between Difference between doGet() and doPost()

DoGet DoPost
In doGet Method the parameters are appended to the URL and sent along with header information In doPost, parameters are sent in separate line in the body
Maximum size of data that can be sent using doget is 240 bytes There is no maximum size for data
Parameters are not encrypted Parameters are encrypted
DoGet method generally is used to query or to get some information from the server Dopost is generally used to update or post some information to the server
DoGet is faster if we set the response content length since the same connection is used. Thus increasing the performance DoPost is slower compared to doGet since doPost does not write the content length
DoGet should be idempotent. i.e. doget should be able to be repeated safely many times This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online.
DoGet should be safe without any side effects for which user is held responsible This method does not need to be either safe

14. Explain JSP/Servle threading model.

Every Servlet contains a doGet or doPost method.
When we deploy a servlet the init method of the servlet is called. And after it has been deployed on each request of the servlet a separate thread is created to process the doGet/doPost method of the servlet.
So all the instance variables of the servlet are not thread safe.
We can override this behaviour by implementing SingleThreadedModel in the servlet which tells the servlet container to create a new Servlet instance for each client’s request instead of using a single Servlet Instance.

But single threaded model is deprecated now in Servlet 2.4 API. API doc says ” Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. ”

this might be the reason why it has been deprecated.

13. what is the default scope of jsp tags?

Page :)-

12. What are the default objects provided by JSP container?

Implicit objects let developers access container-provided services and resources. These objects are defined as implicit because you do not have to explicitly declare them. They are defined in every JSP page and used behind the scenes by the container whether you declare them or not — although you cannot redeclare them. Because implicit objects are declared automatically, we need only use the reference variable associated with a given object to begin calling methods on it.

The nine implicit objects, with a brief description of each one’s function, are:

  • Application is the broadest context state available. It allows the JSP page’s servlet and any Web components contained in the same application to share information.
  • Config allows initialization data to be passed to a JSP page’s servlet.
  • Exception houses exception data to be accessed only by designated JSP “error pages.”
  • Out provides access to the servlet’s output stream.
  • Page is the instance of the JSP page’s servlet processing the current request. Not typically used by JSP page authors.
  • PageContext is the context for the JSP page itself. It provides a single API to manage the various scoped attributes. This API is used extensively when implementing JSP custom tag handlers.
  • Request provides access to HTTP request data, as well as providing a context for associating request-specific data.
  • Response enables direct access to the HTTPServletResponse object and is rarely used by JSP authors.
  • Session is perhaps the most commonly used of the state management contexts. The concept of a “session” is that of a single user interacting with a Web application over several requests.

Although some implicit objects address a single function, several of them provide multiple categories of functionality. In the sections that follow, we’ll review implicit objects according to functional categories:

  • Session management: application, session, request, pageContext
  • Flow control: application, config, pageContext, request, session
  • Logging and exceptions: application, config, exception, pageContext, request, session
  • Input/output control: request, response, out
  • Initialization parameters: config