Вы находитесь на странице: 1из 9

JSP and Servlets Performance Tips: Servlet Performance Tips: Use the servlet init() method to cache static

data, and release them in the destroy() method. 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. Implement the getLastModified() method to use the browser cache and the server cache. Use the application server's caching facility. Session mechanisms from fastest to slowest are: HttpSession, Hidden fields, Cookies, URL rewriting, the persistency mechanism. Remove HttpSession objects explicitly in your program whenever you finish the session. Set the session time-out value as low as possible. Use transient variables to reduce serialization overheads. Disable the servlet auto reloading feature. Tune the thread pool size.

JSP:

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.

Servlets getPathInfo() - path in URL getPathTranslated() - where the filename is located

Setting the header response.setHeader("Content-type","es"); utility native2ascii - converts native codepage to UTF-8 encoding. Useful for .property files and localization

Global attributes this.getServletContext().setAttributes("name", value); - attribute that is visible in the whole application.. for more servlets

JSP <%-- comment <%! - declaration (it stores the code out of the service method) <%@ - directive (affects translation of JSP to Servlet, compilation.. useful for tuning for specific cases like setting the error page, code page imports, etc.) XML syntax of JSP page: ... - same as <%! include

<%@ include file='file.jsp" %> includes file to a page (before translation) - don't recognize if the included file has changed - includes all of the parameters or attributes for included page or < jsp:include page="file.jsp" > dynamic include (during the processing of main page), more flexible - this way we can include result of server or other JSP page - explicit parameter passing to included page by < jsp: param name="name_of_parameter" value="value_of_parameter"> tag. for included pages, there is a need for inserting pages without HTML < body > types ! The page directive <%@ page ... % > useful for setting additional properties for jsp page like isErrorPage="true| false"

plugins usage: < jsp:plugin > ... < /jsp:plugin > it inserts Java applet to the page System environments usage: TERM=vt100 java -D user.term=$TERM then, we can access this environment variable by: System.getProperties() - it returns instance of class Property.

System.getProperties().getProperty("user.term"); System.getProperties().remove("user.term"); JavaBeans requirements for JavaBeans: there have to be set/get methods specified has to have public default constructor (without parameters) it should be in a package serializable get/set methods has to have the same data type! bean cannot modify content of a page!

... in general, JavaBean is a standard Class. It can be used as a simple component in a JSP page. - get method has to be fast, and it should be plain and simple. usage in a JSP page: < jsp:useBean id="nameOfBean" scope="session" class="myPackage.Counter" /> < jsp:getProperty name="nameOfBean" property="myCount" /> this is the same as <%= Counter.myCount %> with JSTL Expression language: < c:out value="${Counter.myCount}" > automatically set the bean attributes from from names (!important!): < jsp:setProperty name="pocetMiest" property="*" />

scope can be: application - the bean is available in all application, when that's in more JSP pages, one of them can create it once and other JSP pages can use it. It's something like a global object for all application.

session - stored data, just for this session request - just for request or forward page - smallest scope, just on a page that is called (forwarded or included)

Custom Tags - reusability - components which you can download and use on your JSP page (3rd party) - Custom tags can also command the pageflow instead of JavaBean, you can for example terminate a page by some custom tag. eg. mytaglib.tld - containts a descriptor of tags in XML usage: < %@ taglib uri="/WEB-INF/jsp/mytaglib.tld" prefix="first" % > some text .... or < %@ taglib uri="mytaglib" prefix="first" % > -> mytablib has to be defined in web.xml - advantage: when changing a tag library, it can be changed only in web.xml instead of changing it manually in every file. < first: something /> some text .... < first:something name="myAttribute" value="myValue" / > -> has to be defined in mytaglib.tld ! in tld file: - < rtexprvalue >true|false< /rtexprvalue > - says that we can use in params < %= expression % > eg.: < first:something name="myAttribute" value="< %= expression % >" / > writing Custom tag class:

- imports javax.servlet.jsp.tagext.*; - class has to implement class Tag or has to extend TagSupport class method doStartTag() - method that is executed when there is a occurency of a start tag. pageContext.getOut.print("I will print this"); // in try/catch block

method doEndTag() - method is executed when there is a closing tag. method getParent() - returns a parent of a current tag; method release() - there we can release some system resources, etc. when there is a body of a tag, it has to implement BodyTagSupport instead of simple TagSupport it means, that you can nest multiple tags within the tag content. method doAfterBody() - for handling nested content BodyContent bc = getBodyContent(); // content of a tag body String body = bc.getString(); // convertion to a String JSPWriter out = bc.getEnclosingWriter(); // enclosing writer of the parent tag.. result can will be written there. possible return values on the end of a tag handling method : return SKIP_BODY; (ignores the body of a custom tag in JSP) - skip to handling method doEndTag() return SKIP_PAGE; - skips the entire following content of a page. return EVAL_BODY_TAG; - evaluates the body of a tag return EVAL_PAGE; - we can continue in page evaluation

JSTL (Java Standard Tag Libraries) - useful tags for creating cycles, we can elliminate scriptlets, - formating messages - internalization

- database communication - expression language expression language eases the manipulation with JavaBeans: eg. ${beanName.propertyName} allow you to evaluate given expression: eg. ${3+5} ${param["foo"]} - HTML parameters from query string ${cookie["myName"]} - cookie with name myName

JavaServer faces UI components for HTML saving it's state between calls we can work with beans on a server component model for HTML quite new technology implements MVC(model / view / controller) pattern as known as Model 2

Sending e-mail

package sun.netsmtp.SmtpClient; SmtpClient smtp = new SmtpClient("...IP.."); smtp.from(from); smtp.to(to); PrintStream msg = smtp.startMesage(); msg.println(....); smtp.closeServer(); - attachments cannot be inserted JavaMail API - more complex

- XSD is exchange for DTD

Вам также может понравиться