hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Amon <argenst...@hotmail.com>
Subject Httpclient 4 inside a JSP TAG
Date Thu, 05 Jan 2012 13:31:03 GMT

Hi all,

I have a question concerning the use of HTTPClient 4 inside a JSP Tag

It's about the creation of the DefaultHttpClient object and how to shutdown the ConnectionManager()
inside a tag.

Currently i am creating a DefaultHttpClient object and shutting it down for each request to
the JSP TAG.

Below a code example:

public class Example Tag extends TagSupport {


    public int doStartTag() throws JspException {
         DefaultHttpClient httpclient = new DefaultHttpClient();
            // Http-request
            HttpGet httpget = new HttpGet(uri);
            // Execute HTTP request
            LOGGER.debug("executing request: " + httpget.getURI());
            HttpResponse response = httpclient.execute(httpget);
            LOGGER.debug("Response status: "+response.getStatusLine());
            if( response.getStatusLine().getStatusCode() == 200) {
                //Get hold of the response entity        
                HttpEntity entity = response.getEntity();
                if (entity != null) {             
                   //write the result to the output...
                }else {
                    throw new JspException ("blabla");
            }else {
                 //Do not feel like reading the response body.            
                throw new JspException ("blabla"+ response.getStatusLine());
        }catch(IOException ex){
            throw new JspException ("blabla",ex);
        }finally {
            //When HttpClient instance is no longer needed,
            //shut down the connection manager to ensure
            //immediate deallocation of all system resources


My question:
1. Is it bad (performace wise,etc) to create for each request a new DefaultHttpClient(); object?
(and to shut it down!)

It's pretty easy to create one DefaultHttpClient(); for the JSP TAG. But there no way to shut
it down properly if the JSP TAG is not used anymore.
(The only way the TAG will be not be used anymore is when the Application is down).

2. It's necessary to call  httpclient.getConnectionManager().shutdown();  inside a tag?

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message