java - Different ways to get Servlet Context -


could explain me difference between ways of getting servletcontext of httpservlet?

doget( httpservletrequest request, ... ){     getservletconfig( ).getservletcontext( );     request.getsession( ).getservletcontext( );     getservletcontext( ); } 

is there difference in performance or in context itself? if so, best way? there other way of retrieving context?

there's 1 more.

request.getservletcontext(); 

there's technically no difference in performance, request.getsession() implicitly create http session object if not created yet. if not done yet, grabbing servlet context via session may take few nanoseconds longer if session isn't created yet.

there's no difference in returned context. methods convenience , method obtain context depends on context ;) you're sitting in.

if you're sitting in method invoked servlet's service() (such doget(), dopost(), etc), use inherited getservletcontext() method. other ways unnecessarily add more characters source code.

@override protected void doget(httpservletrequest request, httpservletresponse response) {     servletcontext context = getservletcontext();     // ... } 

if you're sitting in servlet's init(servletconfig) method, inherited getservletcontext() isn't available yet long haven't called super.init(config). you'd need grab servletconfig.

@override public void init(servletconfig config) {     servletcontext context = config.getservletcontext();     // ... } 

but better override init() instead. in decent servlet never need override init(servletconfig).

@override public void init() {     servletcontext context = getservletcontext();     // ... } 

if you're not sitting in servlet in e.g. filter lacks inherited getservletcontext() method , have servletrequest @ hands, grab there.

@override public void dofilter(servletrequest request, servletresponse response, filterchain chain) {     servletcontext context = request.getservletcontext();     // ... } 

note new since servlet 3.0. previously, you'd have grab session.

@override public void dofilter(servletrequest request, servletresponse response, filterchain chain) {     servletcontext context = request.getsession().getservletcontext();     // ... } 

this not nice if worry unnecessary session creation. hence introduction of servletrequest#getservletcontext() — although extract filterconfig (hey, there's yet way!).

private filterconfig config;  @override public void init(filterconfig config) {     this.config = config; }  @override public void dofilter(servletrequest request, servletresponse response, filterchain chain) {     servletcontext context = config.getservletcontext();     // ... } 

and there http session listeners listen on a.o. session destroy. there's no other way obtain servlet context via httpsession#getservletcontext().

@override public void sessiondestroyed(httpsessionevent event) {     servletcontext context = event.getsession().getservletcontext();     // ... } 

here don't need worry unnecessary session creation because it's @ point created long beforehand. note there's no servletrequest anywhere there's not means of active http request during server side session timeout.

as last, there's servletcontext#getcontext() returns servletcontext of different web application deployed same server (this works if server configured enable cross context access on target webapp).

servletcontext othercontext = context.getcontext("/othercontextpath"); 

but requires current servletcontext start with, should know way use obtain it.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -