Tips for JSP/Servlet programming by Java API from OOP-Research
By Java API from OOP-Research, the development of web applications or WAP services will be easier. A few lines of codes are enough for your JSP or Servlet programming to use XML, JDBC or JavaMail API. Your JSP/Servlet can accept HTTP POST requests with multipart/form-data encode for file upload. And HTTP POST request parameters with Japanese Shift_JIS, Chinese BIG5, Chinese GB2312 or Russian KOI8-R can be decoded easily. Or, the unique session ID can be generated per each user login and a series of HTTP requests can be tracked by this session ID even without Cookie.
We strongly recommend you to read through this article before you start to develop your web applications or WAP services, if your use any of the Java APIs listed below:
OOP XX API is Thread Safe. What does this mean?
This means that: Most of the methods on the core class of our Java API is Thread Safe. In other words, on the single instance of that class, you can call these methods concurrently. This very simple fact will give you so much benefits.
. And only the static variables can be class-scoped. This causes so many garbage-collection of method-scoped variables for those methods. While the modern JVMs in these days are well designed for avoiding the over-heads related with garbage-collection, it is desirable to keep method-scoped variables as less as possible.
Moreover, only the single instance can be shared among the multiple JSP/Servlet within a web application context.
Not only that Thread Safe implementation will result in a significance performance improvement in your JSP/Servlet, it will make your JSP/Servlet simpler ones due to the similar interface of our Java API.
Only one time Initialization
To make a single instance being shared among the multiple JSP/Servlet within a web application context, every core class of our Java APIs implements the static method which always returns the same instance of that class. It is:
This static method works in the 2 different ways, because only the first call of this method needs to initialize the instance of that class, and the subsequent calls must return the same instance as the first call. As for the first call, this method will:
But, all the subsequent calls always return the same instance which is returned by the first call. Thus, this method gives us the way to get the reference to the single instance from anywhere within a web application context. By this method, any JSP or Servlet within the same web application context can share the single instance. Do not use the constructor to initialize the instance of the class. Instead, all you have to do is:
It is easy, isn't it? By the way, you may have noticed that the first call of this method may be somewhat heavy task. Yes, it is desirable to avoid calling this method within:
. Rather, the best place to live for this method is:
and your JSP/Servlet can keep the class-scoped variable for the returned instance. Then,
can call other methods on it, because they are Thread Safe. Anyway, the source fragment of your Servlet will look like this:
The above Servlet gets the single instance of MimeParser, which is the core class of OOP MimeParser. The rest of the Servlets within the same web application may also implement the same init( ) method, and the same instance of MimeParser can be shared among them.
Benefit of load-on-startup in web.xml
, which closes all the JDBC Connections to SQL database. Another example is MailScheduler, the core class of OOP MailScheduler. Its instance keeps the background Thread running, which sends the e-mails at the scheduled time. To start/stop this background Thread, we should call:
independently from the HTTP requests from the web browser. As you know, the best places for these start-up and clean-up tasks are:
of your Servlet. Apache Tomcat (or any other JSP/Servlet server) calls these methods on all the available Servlets in it, at the boot time and before it shutdowns correspondingly. For example, one of your Servlets may implement:
Because both of start-up and clean-up tasks should be done only once, only one Servlet should be responsible for these tasks. The good practice is to define a special kind of Servlet (you can think of it dummy Servlet), whose purposes are:
only. Such a Servlet does not need to implement any of:
Because any HTTP requests will never arrive in that Servlet, the Servlet must be loaded into Apache Tomcat (or any other JSP/Servlet server) explicitly. In the web.xml for your web application context, load-on-startup element can force Apache Tomcat (or any other Servlet server) to load the Servlet at the start-up time of the server. For example, assuming that the class name of the Servlet is InitAndDestroy, your web.xml will include the elements like below:
Then, Apache Tomcat (or any other Servlet server) will load this dummy Servlet and call its init( ) destroy( ) methods. Thus, by the benefit of load-on-startup mechanism, dummy Servlet can play a significant role in your web applications or WAP services while it is invisible to the users.
Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.
ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.