Reputation: 2098
I am trying to improve web application performance. Right now it takes around 3-5 seconds for web form to load on browser but sometimes varies from 3-20 seconds. I want to breakdown time taken by component once Request is dispatched to particular JSP. So that I could analyze if its web form issue or server configuration issue and take corrective majors accordingly.
My understanding is JSPs are compiled into class files when accessed for first time on server and used on subsequent requests.
What happens on server side when request is dispatched to JSP? Any guidance on hardware configuration that might help performance improvement?
Upvotes: 0
Views: 1478
Reputation: 52606
What happens on backend once Request is dispatched to JSP?
A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.
When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.
TRANSLATION AND COMPILATION
During the translation phase each type of data in a JSP page is treated differently. Static data is transformed into code that will emit the data into the response stream. JSP elements are treated as follows:
- Directives are used to control how the web container translates and executes the JSP page.
- Scripting elements are inserted into the JSP page’s servlet class. Expression language expressions are passed as parameters to calls to the JSP expression evaluator.
jsp:[set|get]Property
elements are converted into method calls to JavaBeans components.jsp:[include|forward]
elements are converted into invocations of the Java Servlet API.- The
jsp:plugin
element is converted into browser-specific markup for activating an applet.- Custom tags are converted into calls to the tag handler that implements the custom tag. In the Application Server, the source for the servlet created from a JSP page named pageName is in this file:
domain-dir/generated/jsp/j2ee-modules/WAR-NAME/pageName_jsp.java
.For example, the source for the index page (named index.jsp) for the date localization example discussed at the beginning of the chapter would be named:
domain-dir/generated/jsp/j2ee-modules/date/index_jsp.java
Both the translation and the compilation phases can yield errors that are observed only when the page is requested for the first time. If an error is encountered during either phase, the server will return
JasperException
and a message that includes the name of the JSP page and the line where the error occurred.After the page has been translated and compiled, the JSP page’s servlet (for the most part) follows the servlet life cycle described in Servlet Life Cycle:
1. If an instance of the JSP page’s servlet does not exist, the container: a. Loads the JSP page’s servlet class b. Instantiates an instance of the servlet class c. Initializes the servlet instance by calling the jspInit method 2. The container invokes the `_jspService` method, passing request and response objects. If the container needs to remove the JSP page’s
servlet, it calls the
jspDestroy
method.EXECUTION
You can control various JSP page execution parameters by using page directives. The directives that pertain to buffering output and handling errors are discussed here. Other directives are covered in the context of specific page-authoring tasks throughout the chapter.
BUFFERING
When a JSP page is executed, output written to the response object is automatically buffered. You can set the size of the buffer using the following page directive:
<%@ page buffer="none|xxxkb" %>
A larger buffer allows more content to be written before anything is actually sent back to the client, thus providing the JSP page with more time to set appropriate status codes and headers or to forward to another web resource. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.
HANDLING JSP PAGE ERRORS
Any number of exceptions can arise when a JSP page is executed. To specify that the web container should forward control to an error page if an exception occurs, include the following page directive at the beginning of your JSP page:
<%@ page errorPage="file-name" %>
Read more.
Any guidance on hardware configuration that might help performance improvement?
Buy more RAM and more expensive chip, use SSD hard disk, with CDN (content delivery network) etc.
You don't ask how to tuning performance by programmatic way. Althought, I think this link is helpful for you.
Upvotes: 1
Reputation: 13
JSPs are translated into servlet classes and compiled in runtime or pre-compiled on deployment or packaging (depends on app server etc).
Depends on you app/web server you might have access log or some other metrics available to find out time spend in servlet/jsp. Not too smart but generic solution is to use a servlet filter to capture time spent in servlet.
if it looks acceptable then most likely you have a problem with generated html page rendering. At this stage you need browser level dev tools to see what's going on (dev console in chrome, firebug etc).
Upvotes: 0