wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet design - static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet design - static methods" Watch "Servlet design - static methods" New topic
Author

Servlet design - static methods

Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
Should static methods ever be used when building servlets? I perform HttpUrlConnections in a static method of a Java bean. None of the variables are static, just the method. The Tomcat server that my servlet is on keeps leaving threads hanging and I'm afraid it's because of this.

I've read that having static methods in classes that only perform calculation on data passed to it are perfectly acceptable. However, I'm not sure if that's only true for stand alone Java apps or for servlets as well. The fact that I'm connecting to external web sites from within my static method leaves me even more concerned.

The servlet displays some XML that it retrieves from another website. I need to submit an HTTP POST request containing some XML to query the data. The static method(s) submit the request and get the response. There is one to submit the request, and one to get the response.

The first static method accepts a String holding the URL and the XML query in a byte array. I think the try block should be located in the actual method. I copied this from some other code earlier this year when I was still pretty fresh with Java. Here is the code:



the try block in the servlet then passes the httpConn to the getSOAPResponse method:



Some of the results can be WAY too big, so I continually check the size of the character array. If it is bigger than 3.5 million, I return the String "-1", display an error, and "return" to terminate execution.

The calling code in the servlet is wrapped in a try block and looks like this:


Here is a list of my concerns:
1) The methods are static. I'm not sure the JVM is doing any automatic clean up of anything.
2) The methods throw Exception. I think they should be wrapped in try blocks inside the method, not inside the servlet. Just a guess, but this might leave the readers open when it jumps out and, given that it's a static method, they are left hanging.
3) Even though the request/response actually goes through, I accidently set the request method to "GET" instead of "POST". Not sure if that has any negative effect.
4) At the end of the getSOAPResponse method, I close the BufferedReader, but not the InputStreamReader.
5) If the size goes over 3.5 million, I don't close anything and return.

Instead of using multiple static methods in a seperate class to sumbit the request and then get the response, would a better approach be to create a class specifically to perform the entire query, wrapped in a try block that uses finally to close everything, and have a private instance variable to store the final string, and a get method to retrieve it? Do I need to close the BufferedReader, InputStreamReader, and HttpURLConnection? Do I only need to call the .close() methods, or should I set these to null?

Thanks for your response!


SCJP 5.0
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet design - static methods
 
Similar Threads
Problems connecting to LDAP from Weblogic Server: BAD_CERTIFICATE error!
Sending http POST Data and XML
Connection timeout with HttpsURLConnection
Invoking webservice- Server returned HTTP response code: 500
Web service using HttpURLConnection