How to Improve JSP Page performance

  • Use the jspInit() method to cache static data, and release them in the jspDestroy() method.
  • Use the jspInit() method to cache static data.
  • Use StringBuffer rather than using + operator when you concatenate multiple strings.
  • Use the print() method rather than the println() method.
  • Use a ServletOutputStream rather than a PrintWriter to send binary data.
  • Initialize the PrintWriter with the optimal size for pages you write.
  • Flush the data in sections so that the user can see partial pages more quickly.
  • Minimize the synchronized block in the service method.
  • Avoid creating a session object with the directive <%@ page session=”false” %>
  • Increase the buffer size of System.out with the directive <%@ page buffer=”12kb” %>
  • Use the include directive instead of the include action when you want to include another page.
  • Minimize the scope of the ‘useBean’ action.
  • Custom tags incur a performance overhead. Use as few as possible.
  • Use the application server’s caching facility, and the session and application objects (using getAttribute()/setAttribute()). There are also third-party caching tags available.
  • Session mechanisms from fastest to slowest are: session, Hidden fields, Cookies, URL rewriting, the persistency mechanism.
  • Remove ‘session’ objects explicitly in your program whenever you finish the session.
  • Reduce the session time-out as low as possible.
  • Use ‘transient’ variables to reduce serialization overheads.
  • Disable the JSP auto reloading feature.
  • Tune the thread pool size.
Advertisements

40. What is the difference between <jsp:include> and <c:import>

  1. The <c:import> tag imports a resource specified by a URL and exposes it to the page, variable, or reader.
  2. The <jsp:include> standard action is a request-time action that will include the output of another JSP at the location of the tag within the calling JSP.
  3. <c:import> is similar to the <jsp:include> directive, but with more features.
  4. The body of the import tag can have param tags for adding parameters to the URL.
  5. we can overcome jsp:include limitations with the JSTL import tag.
  6. The import tag adds functionality to the jsp:include tag and eliminates the need to code these functions yourself.
  7. The first additional function allows you to include content directly from other Web sites. So the following is now legal:
    <c:import url=”
    http://www.example.com/somecontent.html”/>
  8. You can even use ftp:
    <c:import url=”
    ftp://ftp.example.com/somecontent.txt”/>
  9. Rather than dump an imported URL straight to the page, you can store it in a variable. This is a common strategy in all the JSTL tags and increases the power of a JSP page:
    <c:import url=”http://www.example.com/example.inc” var=”example”/> Once this included page is stored in the example variable, you can use another taglib, such as the Jakarta String taglib, to replace all references to example.com with done.com.
  10. Since you can store <c:import> in a variable I’m wondering if <c:import> might do some type of full read before printing to output, whereas I imagine <jsp:include> uses buffering. This could show a performance difference for very large files, with <c:import> requiring longer and taking more memory to get all the data in the import whereas <jsp:include> did things with buffers.
  11. Include Syntax:

<jsp:include page=”…url..” flush=”true or false“/>

The tag can optionally contain a body:

<jsp:include page="...url..." flush="true or false">
  <jsp:param  ..../>
</jsp:include>

          Import Syntax:

<c:import url=”resource URL”
          [var=”variable name”]
          [scope=”page|request|session|application”]
          [varReader=”variable name”]
          [context=”context name”]
          [charEncoding=”character encoding”]>
     JSP body content
</c:import>

36. Sir, do you know about jsp application object

  • This implicit object represents the application to which the JSP belongs.
  •  JSPs are grouped into applications according to their URLs where the first directory name in a URL defines the application.
  • The application object contains methods that provide information about the JSP Container, support for logging plus utility methods for example translating a relative URL to an absolute path.
Servlet Class

javax.servlet.ServletContext

The following table summarises the most useful methods available to the application object.

Method Description
getAttribute(String objName) Returns the object named objName or returns null if the objName does not exist.
getAttributeNames() Returns an Enumeration containing the attribute names available within the application.
setAttribute(String objName, Object object) Stores the object with the given object name in this application.
removeAttribute(String objName) Removes the objName object from the application.
getMajorVersion() Returns the major version of the Servlet API for the JSP COntainer.
getServerInfo() Returns information about the JSP Container, typically, name and product version.
getResourceAsStream(Path) Translates the resource URL into an input stream for reading.
log(Message) Writes a text string to the JSP Containers default log file.

35. Tell me something about jsp session object

  • The session implicit object is used to provide an association between the client and the server.
  • This association, or session, persists over multiple connections and/or requests during a given time period.
  • Sessions are used to maintain state and user identity across multiple page requests.
  • A session can be maintained either by using cookies or by URL rewriting. To expose whether the client supports cookies, session defines an isCookieSupportDetermined() method and an isUsingCookies() method.
Servlet Class

HttpSession

The following table summarises the most useful methods available to the session object.

Method Description
isNew() A session is considered to be “new” if it has been created by the server, but the client has not yet acknowledged joining the session.
invalidate() Discards the session, releasing any objects stored as attributes.
getAttribute(String) Retrives the object associated with the named attribute.
getAttributeNames() Retrives the names of all attributes currently associated with the session.
setAttribute(String, object) Sets the object to the named attribute. attribute created if it doesn’t exist.
removeAttribute(String) Removes the object bound with the specified name from this session.

34. What do you know about jsp response object

  • The response object handles the output to the client.
  • This object can be used for creating HTTP Headers, creating cookies, setting content type and redirecting workflow.
Servlet Class

javax.servlet.http.HttpServletResponse

The following table summarises the most useful methods available to the response object.

Method Description
setContentType() Sets the MIME type and character encoding for the page.
addCookie(Cookie) Adds the specified cookie to the response. It can be called multiple times to set more than one cookie.
containsHeader(name) Checks whether the response already includes this header.

 

setHeader(name, value) Creates an HTTP Header.
sendRedirect(String) Sends a temporary redirect response to the client using the specified redirect location URL.

33. Explain request implicit object

  1. The request object retrieves the values that the client browser passed to the server during an HTTP request such as headers, cookies or parameters associated with the request.
  2. Among the most common use of the request object is to obtain parammeter or query string values.

Servlet Class

javax.servlet.HttpServletRequest

The following table summarises the most useful methods available to the request object.

Method Description
getParameter(String name) Returns the value of a request parameter as a String, or null if the parameter does not exist.
getParameterNames() Returns an Enumeration of String objects containing the names of the parameters contained in this request.
getParameterValues(String name) Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
getCookies() Gets the array of cookies found in this request. See below for more details on working with cookies.
getQueryString() Gets any query string that is part of the HTTP request URI.
getRequestURI() Gets the URI to the current JSP page.
getHeaderNames() Returns an enumerator of all HTTP header names.
getHeader(String hdr) Returns the value of an HTTP header such as “QUERY_STRING” or “URL”. The method getHeaderNames() can be used to determine what headers are available. See example below.
getAttribute(String) Retrives the object associated with the named attribute.
getAttributeNames() Retrives the names of all attributes currently associated with the session.
setAttribute(String, object) Sets the object to the named attribute. attribute created if it doesn’t exist.
removeAttribute(String) Removes the object bound with the specified name from this session.

30. Explain the advantages of JSTL

  1. The Java Standard Tag Library (JSTL) is a standardized collection of custom tags.
  2. It has a number of tags for common tasks such as iterating through lists, interacting with databases, handling XML data, formatting data, and much more.
  3. The latest version of JSTL is 1.1. An implementation of JSTL can be downloaded from the Apache Jakarta Web site (http://jakarta.apache.org/).
  4. JSTL consist of four sets of tags and a library of functions, grouped according to their functionality:
  • The JSTL core tag library contains tags that deal with flow control in JSP pages, iterating over collections, evaluating expressions, and importing resources.
  • Before using the core tag library, the following directive needs to be added to the JSP page:
  • <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; %>
  • The equivalent XML syntax for this directive is as follows:
  • <jsp:directive.taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; />

The following table summarizes the JSTL core tags.

Tag Name

Description

catch

Catches any Throwable exception that occurs in the body of the tag

choose

Provides conditional operation and allows for choosing between mutually exclusive options

if

Provides conditional operation

import

Imports a resource specified by a URL and exposes it to the page, variable, or reader

forEach

Iterates over collections

forTokens

Iterates over tokens

out

Outputs expressions to the Web page

otherwise

A conditional operation tag for choosing between mutually exclusive options. This tag is a subtag of the choose tag.

param

Used along with the import, redirect, or url tags to add a parameter to a URL

redirect

Redirects to a new URL

remove

Removes a variable from a scope

set

Sets the value of an attribute

url

Creates a URL

when

A conditional operation tag that includes its body when an expression evaluates to true. This tag is a subtag of the choose tag.