• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet design - static methods

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic