Thursday, 19 June 2008

Web Development with Java By Tim Downey

Just now i completed reading
Web Development with Java Using Hibernate,JSPs and Servlets
By Tim Downey

Below are the points i wanted to share with readers from this book.

I think this book is useful for starters who are new to web-programming.

1) Request Format

The request from the browser has the following format in HTTP:

a. The first line contains the type of request, the name of the requested page and the protocol that is being used.
b. Subsequent lines contain information about the browser and the request.
c. A blank line indicates the end of the request headers.
d. In a POST request, there can be additional information sent after the blank line.

2) Response Format

The response from the server has the following format in HTTP:

a. The first line contains the status code, a brief description of the status code and the protocol being used.
b. Subsequent lines contain information about the server and the response.
c. A blank line indicates the end of the response headers.
d. In a successful response, the content of the page will be sent after the blank line.

3) In the servlet specification 2.0, there is a new language that has been added to JSPs that simplifies access to objects that are available to a JSP. This language is known as the Expression Language [EL]. EL statements start with a dollar sign and are surrounded by curly braces.

${EL-statement}

4) Member variables in the servlet can be accessed by all of the threads. If two threads attempt to save a value to a member variable, only the value written by the last thread will be stored.The problem with member variables makes them dangerous to use in a servlet.Think of member variables in a servlet as being more like static variables in a simple Java program. Because of this, it is better to avoid using member variables in a servlet.

5) Programmers must resist the desire to create member variables in servlets to avoid passing parameters to methods. Even though object-oriented design promotes the use of member variables, it can cause intermittent errors when used in a servlet.It is difficult to create an example that always produces incorrect answers, but with enough requests, most servlets that use member variables will have errors.Of course, it is possible to use member variables if the intent is to share data across all requests. In this case, care must be taken to synchronize all access to the shared variable. This example demonstrates that something as simple as adding 1 to a shared variable can produce incorrect answers.

6) Controller: Shared Variable Error

The controller for this application is very simple. It always transfers control to the same JSP. The only work that it performs is to increment a member variable by 1.In order to cause an error each time a request is processed, the work has been placed in a helper method.


protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

String address = jspLocation() +Edit.jsp;
incrementSharedVariable();

request.setAttribute(accessCount”, accessCount);
request.getRequestDispatcher(address)

.forward(request, response);
}
...

}


Describing the error is easier than causing the error in a servlet. Such an error will only occur if two requests are made very close to each other. Usually, this will
occur in a servlet that receives many requests. There is a low probability that this
error will occur, but, with enough requests, it can happen.

In this example, the value of the shared variable is copied into a local, private variable; the local variable is then incremented and the thread is put to sleep; the thread copies the local variable back to the shared variable when it wakes up.

This mimics the action of the thread copying the value of a variable into the CPU; the thread incrementing the value and being interrupted by the CPU; the thread writing the value in the CPU back to memory when it regains control.

The advantage of doing this is to be able to set the length of time that the thread sleeps. Instead of losing control for a few milliseconds, we can force the thread to sleep for many seconds. This will give us slow humans the ability to cause this error every time.


...
public int accessCount = 0;
...
public void incrementSharedVariable() {
int temp = accessCount;
temp++;
System.out.println(temp);
try {
Thread.sleep(3000);
} catch (java.lang.InterruptedException ie) {
}
accessCount = temp;
}
...


Open two different browsers, not just two instances of the same browser, as Tomcat does not allocate a new thread to the same servlet from the same browser. After doing this, you will see that both instances display the same number, even though both of them were incrementing the same shared variable.

There are two solutions to this problem: synchronize access to the shared variable and avoid using member variables in servlets.

7) The context listener interface only has two methods: one that is called when the web application starts and one that is called when the web application stops. Define a class that implements this interface.

The context listener must be called by the servlet engine when the web application starts. This is done by adding some tags to the web.xml file. The only parameter that is needed is the class for the above listener class.


<listener>
<listener-class><<<some class>>></listener-class>
</listener>



When the web application starts, it will load the listener class into memory and the contextInitialized method will be called. The listener class will remain in memory as long as the web application is running. When the web application receives a command to stop, it will first call the contextDestroyed method.

8) Closing Hibernate is imperative. Please remember that in weblogic containers this happens automatically.So please check your container documentation whether this happens automatically.If the web application closes without releasing the resources that Hibernate uses, there will be a memory leak in the servlet engine.

There is an interface named ServletContextListener that has two methods that will be called when the web application starts and when the web application stops. By adding a class to the web application that implements the interface and confi guring the web.xml file to call this class, Hibernate can be closed
only when the web application is stopped.


9) Static variables will not be garbage collected until the class is removed from memory.

About the Author

Tim Downey says,

I have been teaching web development for ten years. I started with Perl.I can still remember the behemoth programs that contained all the logic and HTML. I remember using a text editor to write the program.
Debugging consisted of a lot of print statements. It was a fun time, full of exploration, but I do not miss them.

Five years ago, I made the move to Java and Java servlets. Life became much simpler with the use of NetBeans. It has been a critical component in developing Web applications using Java. Debugging a web application
in NetBeans is just as easy as debugging any Java application.

This book is meant for students who have a solid background in programming, but who do not have any database training. Until two years ago, my students used a glorified HashMap to save data. Then a former student gave me the word: Hibernate. For anyone with a programming background in Java, using Hibernate to save data to a relational database is a simple task.

Hope you enjoy reading this book

No comments: