What are the lifecycle methods of a JSP?

A JSP has following lifecycle methods:

<li><strong>jspInit()</strong>: This method is invoked when the JSP is called for the first time. We can do initial setup for servicing a request in this method.</li>

<li><strong>jspService()</strong>: This method is used to serve every request of the JSP.</li>

<li><strong>jspDestroy()</strong>: Once we remove a JSP from the container, we call this method. It is used for cleanup of resources like Database connections etc. </li>

What are the advantages of using JSP in web architecture?

We get following advantages by using JSP in web architecture:

  • Performance: JSP provides very good performance due to their design of using same code to service multiple requests.
  • <li><strong>Fast</strong>: Since JSP is pre-compiled, server can serve the pages very fast.</li>
    <li><strong>Extendable</strong>: JSP is based on Java Servlets. This helps in extending JSP architecture with other Java technologies like JDBC, JMS, JNDI etc.</li>
    <li><strong>Design</strong>: It is easier to design user interface with JSP, since it is very close to HTML. UI designers can create a JSP with mock data and developers can later provide implementation of dynamic data.</li>

    What is the advantage of JSP over Javascript?

    In JSP we can write Java code seamlessly. It allows for writing code that can interact with the rest of the application.

    Javascript code is mostly executed at client side. This limits the tasks that can be done in Javascript code. We cannot connect to database server from Javascript at the client side.

    What is the Lifecycle of JSP?

    JSP has following lifecycle stages:

  • Compilation: When a request is made for a JSP, the corresponding JSP is converted into Servlet and compiled. If there is already a compiled form of JSP and there is not change in JSP page since last compilation, this stage does not do anything.
  • <li><strong>Initialization</strong>: In this stage, jspInit() method is called to initialize any data or code that will be later used multiple times in _jspService() method. </li>
    <li><strong>Service</strong>: In this stage, with each request to JSP, _jspService() method is called to service the request. This is the core logic of JSP that generates response for request.</li>
    <li><strong>Destroy</strong>: In this stage, JSP is removed from the container/server. Just before removal, this stage performs the cleanup of any resources held by JSP.</li>

    What is a JSP expression?

    A JSP expression is an element of a JSP page that is used to evaluate a Java expression and convert into a String. This String is replaced into the locations wherever the expression occurs in JSP page.

    • E.g.<%= expression =%>.

    How will you prevent multiple submits of a page that come by clicking refresh button multiple times?

    We can use Post Redirect Get (PRG) pattern to solve the issue of multiple submission of same data. It works as follows:

    First time when a user submits a form to server by POST or GET method, then we update the state in application database.

    Then we send a redirect response to send reply to client.

    Then we load a view by using GET command. There is no data is sent in this. Since this a new JSP page, it is safe from multiple submits. The code that processes the request is idempotent. So it does not do same action twice for same request.