How can you make a session listener for HttpClient?

On Thu, Jan 5, 2012 at 5:00 PM, William Speirs <[email protected]> wrote:

> Could you setup a session listener and call create/shutdown there?
>
> Bill-
>
> On Thu, Jan 5, 2012 at 8:31 AM, Amon <[email protected]> wrote:
> >
> > 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 {
> >
> > .....
> >
> >    @Override
> >    public int doStartTag() throws JspException {
> >
> >         DefaultHttpClient httpclient = new DefaultHttpClient();
> >        try{
> >
> >            // 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.
> >                httpget.abort();
> >                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
> >            httpclient.getConnectionManager().shutdown();
> >        }
> >    }
> >
> > }
> >
> >
> > 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?
> >
> >
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>

Reply via email to