Servlet Best Practices

Connect with

Oracle Java
Servlet-based technologies designed to make web application development easier and more effective. The first area of innovation has been happening at the presentation layer. Technologies such as Spring Framework, Struts, JavaServer Pages (JSP), WebMacro, and Velocity give us more productive alternatives. I advise you to consider using a framework and provide some helpful tips in this section on selecting the right framework.

Use Servlet as Controller

User Servlet always as Front Controller (FC), subcontroller, I mean to say , it should be controller somehow not used as View. In the MVC (Model, View and Controller ) stick using of servlet as Controller not as View.

Use Pre-Encoded Characters

Its good to have use pre-encoded character in the Servlet. Thumb rule of whether , we should use PrintWriter or OutputStream , is use a PrintWriter for writing characters and an OutputStream for writing bytes.

A PrintWriter has a downside, specifically, it has to encode every character from a char to a byte sequence internally. When you have content that’s already encoded—such as content in a file, URL, or database, or even in a String held in memory—it’s often better to stick with streams. That way you can enable a straight byte-to-byte transfer. Except for those rare times when there’s a charset mismatch between the stored encoding and the required encoding, there’s no need to first decode the content into a String and then encode it again to bytes on the way to the client. pre-encoded play a lot of role in performance of servlet, use the pre-encoded characters and you can save a lot of overhead.

Performance on an average depends on container to container approx 20% .

Load Configuration Files from the Classpath

place per application configuration files in WEB-INF/classes found in the resource path. It also works equally well for locating configuration files placed within WAR files and/or deployed across multiple back-end servlet containers.

HTTPSession usage

User HttpSession with minimum or optimum , it means use wherever required.
when should we use HttpSession is good question, consider following while using HttSession.

  • Storing login status: The timeout is useful, and changes between browsers or machines should naturally require a new login.
  • Storing user data pulled from a database : The local cache avoids an across-the-wire database request.
  • Storing user data that’s temporary: Temporary data includes search results, form state, or a guest’s shopping-cart contents that don’t need to be preserved over the long term.

Don’t Use SingleThreadModel

Think several times before using this , I mean to say, use this when you have sufficient reason to have it. This interface was intended to make life easier for programmers concerned about thread safety, but the simple fact is that SingleThreadModel does not help.

Use of Caching

Pregeneration and caching of content can be key to providing your site visitors with a quality experience. So , use wherever required, specially in Restful Web Services.

// This content will expire in 24 hours.
long t = System.currentTimeMillis() ;
response.setDateHeader("Expires", t + 24*60*60*1000);
response.setDateHeader("Date", new Date());

Choose the Right Servlet Framework

Choosing of framework is not easy , specially when you have a lot of options available.

Tips for selecting a framework

Before choosing a servlet framework, it’s important that you should consider its feature list. Following are some of the features that frameworks provide. Not all frameworks support all these features, nor should this short list be considered exhaustivly.

  • Form validation: Frameworks commonly provide tools to validate form data, allowing the framework to sanity-check parameters before the servlet even sees the data, for example. Some allow for easy development of “form wizards” with Next/Previous buttons and maintained state.
  • Error handling: Some frameworks include advanced or custom error handling, such as sending alert emails, logging errors to a special data store, or autoformatting errors to the user and/or administrator.
  • Internationalization & Nationalization : Internationalization (i18n) and Nationalization (n10n) is always a challenge, but some frameworks have features and idioms that simplify the process.
  • Integration with a template language: Some frameworks integrate with a specific template language. Others have a pluggable model to support many templates, although they’re often optimized for one template language. If you prefer a particular template language, make sure the framework supports it well.
  • Security integration: The default servlet access control and security model works for simple tasks but isn’t extensible for more advanced needs. Some frameworks provide alternative security models, and many support pluggable security models.
  • Support of designer/developer separation: One of the common goals in web application development is to effectively separate the developer’s duties from the designer’s. Choosing the right template language helps here, but the framework choice can have even more impact. Some enable the separation of concerns, some enforce it.
  • Mechanisms to support web services: With the growing interest in web services, it’s common to see new frameworks centered around web services feature.
  • Reference


    Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *