How to get Server/Servlet Context without HttpServlet or HttpServletRequest? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • Using special auto start servlet to initialize on startup and share application data

    1 answer




Suppose I host a JavaEE WebApp on Payara/Glassfish (or any other WebContainer for that matter).
I would reach the Servlets under the address https://a.b/appname/myservleturlpattern or as https://a.b/myservleturlpattern if the VirtualServer-Config points to the app as default.



Usually I can get that address easily inside any HttpServlet (ServletContext).



But now, when I'm running a Background task (@Schedule) in a non-servlet class, how would I get that address?



What I want: get the app's base address https://a.b/appname/ or as https://a.b/ from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)



So far, my only idea is to listen to all my Servlets (extending the same custom HttpServlet anyway) and on the first call set a static variable, but thats really really unclean and hack-isch.



Any better ideas?










share|improve this question















marked as duplicate by BalusC java-ee
Users with the  java-ee badge can single-handedly close java-ee questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 20:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What do you want from the servlet context?
    – Lev Kuznetsov
    Nov 8 at 11:29










  • What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
    – JayC667
    Nov 8 at 12:23






  • 1




    You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
    – Lev Kuznetsov
    Nov 8 at 12:35










  • Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
    – JayC667
    Nov 8 at 12:38












  • Proper Linkt to other topic: stackoverflow.com/questions/675730/…
    – JayC667
    Nov 8 at 12:51















up vote
0
down vote

favorite













This question already has an answer here:




  • Using special auto start servlet to initialize on startup and share application data

    1 answer




Suppose I host a JavaEE WebApp on Payara/Glassfish (or any other WebContainer for that matter).
I would reach the Servlets under the address https://a.b/appname/myservleturlpattern or as https://a.b/myservleturlpattern if the VirtualServer-Config points to the app as default.



Usually I can get that address easily inside any HttpServlet (ServletContext).



But now, when I'm running a Background task (@Schedule) in a non-servlet class, how would I get that address?



What I want: get the app's base address https://a.b/appname/ or as https://a.b/ from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)



So far, my only idea is to listen to all my Servlets (extending the same custom HttpServlet anyway) and on the first call set a static variable, but thats really really unclean and hack-isch.



Any better ideas?










share|improve this question















marked as duplicate by BalusC java-ee
Users with the  java-ee badge can single-handedly close java-ee questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 20:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What do you want from the servlet context?
    – Lev Kuznetsov
    Nov 8 at 11:29










  • What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
    – JayC667
    Nov 8 at 12:23






  • 1




    You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
    – Lev Kuznetsov
    Nov 8 at 12:35










  • Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
    – JayC667
    Nov 8 at 12:38












  • Proper Linkt to other topic: stackoverflow.com/questions/675730/…
    – JayC667
    Nov 8 at 12:51













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Using special auto start servlet to initialize on startup and share application data

    1 answer




Suppose I host a JavaEE WebApp on Payara/Glassfish (or any other WebContainer for that matter).
I would reach the Servlets under the address https://a.b/appname/myservleturlpattern or as https://a.b/myservleturlpattern if the VirtualServer-Config points to the app as default.



Usually I can get that address easily inside any HttpServlet (ServletContext).



But now, when I'm running a Background task (@Schedule) in a non-servlet class, how would I get that address?



What I want: get the app's base address https://a.b/appname/ or as https://a.b/ from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)



So far, my only idea is to listen to all my Servlets (extending the same custom HttpServlet anyway) and on the first call set a static variable, but thats really really unclean and hack-isch.



Any better ideas?










share|improve this question
















This question already has an answer here:




  • Using special auto start servlet to initialize on startup and share application data

    1 answer




Suppose I host a JavaEE WebApp on Payara/Glassfish (or any other WebContainer for that matter).
I would reach the Servlets under the address https://a.b/appname/myservleturlpattern or as https://a.b/myservleturlpattern if the VirtualServer-Config points to the app as default.



Usually I can get that address easily inside any HttpServlet (ServletContext).



But now, when I'm running a Background task (@Schedule) in a non-servlet class, how would I get that address?



What I want: get the app's base address https://a.b/appname/ or as https://a.b/ from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)



So far, my only idea is to listen to all my Servlets (extending the same custom HttpServlet anyway) and on the first call set a static variable, but thats really really unclean and hack-isch.



Any better ideas?





This question already has an answer here:




  • Using special auto start servlet to initialize on startup and share application data

    1 answer








java-ee web-applications






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:23

























asked Nov 8 at 11:12









JayC667

659718




659718




marked as duplicate by BalusC java-ee
Users with the  java-ee badge can single-handedly close java-ee questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 20:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by BalusC java-ee
Users with the  java-ee badge can single-handedly close java-ee questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 20:32


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What do you want from the servlet context?
    – Lev Kuznetsov
    Nov 8 at 11:29










  • What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
    – JayC667
    Nov 8 at 12:23






  • 1




    You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
    – Lev Kuznetsov
    Nov 8 at 12:35










  • Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
    – JayC667
    Nov 8 at 12:38












  • Proper Linkt to other topic: stackoverflow.com/questions/675730/…
    – JayC667
    Nov 8 at 12:51


















  • What do you want from the servlet context?
    – Lev Kuznetsov
    Nov 8 at 11:29










  • What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
    – JayC667
    Nov 8 at 12:23






  • 1




    You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
    – Lev Kuznetsov
    Nov 8 at 12:35










  • Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
    – JayC667
    Nov 8 at 12:38












  • Proper Linkt to other topic: stackoverflow.com/questions/675730/…
    – JayC667
    Nov 8 at 12:51
















What do you want from the servlet context?
– Lev Kuznetsov
Nov 8 at 11:29




What do you want from the servlet context?
– Lev Kuznetsov
Nov 8 at 11:29












What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
– JayC667
Nov 8 at 12:23




What I want: get the app's base address a.b/appname or as a.b from any class, especially from non-Servlet classes. (Cant get and don't need the myservleturlpattern because I'm not in a servlet.)
– JayC667
Nov 8 at 12:23




1




1




You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
– Lev Kuznetsov
Nov 8 at 12:35




You can't get that information without a request. The container isn't aware of any of the routes a client may reach it, which is actually almost always numerous. It's available with a request because the client tells the container how it called the container. If you have to have this information I suggest you instead make an endpoint available and use a cron job to call it externally. Ideally your app should not depend on this, it makes deployments difficult. Your own solution doesn't work: what if no requests are made before the schedule?
– Lev Kuznetsov
Nov 8 at 12:35












Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
– JayC667
Nov 8 at 12:38






Yeah, I know that my solution is not the best. Ticket stackoverflow.com/questions/53206594/… has exactly the same problems as I do... Thought I can guarantee that I will not be behind load balancing or proxies.
– JayC667
Nov 8 at 12:38














Proper Linkt to other topic: stackoverflow.com/questions/675730/…
– JayC667
Nov 8 at 12:51




Proper Linkt to other topic: stackoverflow.com/questions/675730/…
– JayC667
Nov 8 at 12:51












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










ServletContextListener serves exactly this purpose but there may be better solutions depending on what you want to do with with it






share|improve this answer





















  • Awesome! Thanx! Didnt think about the initializers...
    – JayC667
    Nov 8 at 12:33


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










ServletContextListener serves exactly this purpose but there may be better solutions depending on what you want to do with with it






share|improve this answer





















  • Awesome! Thanx! Didnt think about the initializers...
    – JayC667
    Nov 8 at 12:33















up vote
1
down vote



accepted










ServletContextListener serves exactly this purpose but there may be better solutions depending on what you want to do with with it






share|improve this answer





















  • Awesome! Thanx! Didnt think about the initializers...
    – JayC667
    Nov 8 at 12:33













up vote
1
down vote



accepted







up vote
1
down vote



accepted






ServletContextListener serves exactly this purpose but there may be better solutions depending on what you want to do with with it






share|improve this answer












ServletContextListener serves exactly this purpose but there may be better solutions depending on what you want to do with with it







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 11:38









Lev Kuznetsov

1,8751923




1,8751923












  • Awesome! Thanx! Didnt think about the initializers...
    – JayC667
    Nov 8 at 12:33


















  • Awesome! Thanx! Didnt think about the initializers...
    – JayC667
    Nov 8 at 12:33
















Awesome! Thanx! Didnt think about the initializers...
– JayC667
Nov 8 at 12:33




Awesome! Thanx! Didnt think about the initializers...
– JayC667
Nov 8 at 12:33



Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff