On Thu, 2012-01-05 at 15:59 -0500, William Speirs wrote: > I haven't done this type of thing in a while, but basically you have > the reference to the HttpClient created by the SessionListener and > then destroyed (shutdown) by it as well. If a request comes in that > needs the client it simple gets it from the session and uses it. > > http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionListener.html > > Bill- >
I would even recommend using a ServletContextListener: create an instance of HttpClient in #contextInitialized(), get the instance from the servlet context whenever needed, shut down the connection manager in #contextDestroyed() when no longer needed. Oleg > 2012/1/5 Yakup YÜCE <yyuc...@hotmail.com>: > > How can you make a session listener for HttpClient? > > > > On Thu, Jan 5, 2012 at 5:00 PM, William Speirs <wspe...@apache.org> wrote: > > > >> Could you setup a session listener and call create/shutdown there? > >> > >> Bill- > >> > >> On Thu, Jan 5, 2012 at 8:31 AM, Amon <argenst...@hotmail.com> 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: httpclient-users-unsubscr...@hc.apache.org > >> For additional commands, e-mail: httpclient-users-h...@hc.apache.org > >> > >> > >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org > For additional commands, e-mail: httpclient-users-h...@hc.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org