Receiving ServletContext Into A Plain Java Class?
I'm just starting out learning some JSP and servlets today and was wondering if it's possible to get the session's ServletContext
as a variable and pass it to a plain Java class? If so, how may I do that?
My simple servlet:
public class myServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
//How do I receive the servlet context below in a plain Java class?
ServletContext sc = session.getServletContext();
request.setAttribute("sc", sc);
}
}
My Java class is just a plain one:
public class myClass extends HttpServlet {
//I want to be able to use the ServletContext as a variable that is passed from myServlet class into this one.
}
In myClass
I want to be able to use it to get the real path file of a file within my project:
ServletContext sc
String path = sc.getRealPath(...)
EDIT: Can I do something like this in myServlet
servlet?:
String realPath = sc.getRealPath("/WEB-INF/myFile");
But then how do I pass this realPath
variable into myClass
so I can use it there instead of in myServlet
?
java jsp session servlets realpath
add a comment |
I'm just starting out learning some JSP and servlets today and was wondering if it's possible to get the session's ServletContext
as a variable and pass it to a plain Java class? If so, how may I do that?
My simple servlet:
public class myServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
//How do I receive the servlet context below in a plain Java class?
ServletContext sc = session.getServletContext();
request.setAttribute("sc", sc);
}
}
My Java class is just a plain one:
public class myClass extends HttpServlet {
//I want to be able to use the ServletContext as a variable that is passed from myServlet class into this one.
}
In myClass
I want to be able to use it to get the real path file of a file within my project:
ServletContext sc
String path = sc.getRealPath(...)
EDIT: Can I do something like this in myServlet
servlet?:
String realPath = sc.getRealPath("/WEB-INF/myFile");
But then how do I pass this realPath
variable into myClass
so I can use it there instead of in myServlet
?
java jsp session servlets realpath
why are your extendingpublic class myClass extends HttpServlet
?
– Scary Wombat
Nov 15 '18 at 4:14
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18
add a comment |
I'm just starting out learning some JSP and servlets today and was wondering if it's possible to get the session's ServletContext
as a variable and pass it to a plain Java class? If so, how may I do that?
My simple servlet:
public class myServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
//How do I receive the servlet context below in a plain Java class?
ServletContext sc = session.getServletContext();
request.setAttribute("sc", sc);
}
}
My Java class is just a plain one:
public class myClass extends HttpServlet {
//I want to be able to use the ServletContext as a variable that is passed from myServlet class into this one.
}
In myClass
I want to be able to use it to get the real path file of a file within my project:
ServletContext sc
String path = sc.getRealPath(...)
EDIT: Can I do something like this in myServlet
servlet?:
String realPath = sc.getRealPath("/WEB-INF/myFile");
But then how do I pass this realPath
variable into myClass
so I can use it there instead of in myServlet
?
java jsp session servlets realpath
I'm just starting out learning some JSP and servlets today and was wondering if it's possible to get the session's ServletContext
as a variable and pass it to a plain Java class? If so, how may I do that?
My simple servlet:
public class myServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
//How do I receive the servlet context below in a plain Java class?
ServletContext sc = session.getServletContext();
request.setAttribute("sc", sc);
}
}
My Java class is just a plain one:
public class myClass extends HttpServlet {
//I want to be able to use the ServletContext as a variable that is passed from myServlet class into this one.
}
In myClass
I want to be able to use it to get the real path file of a file within my project:
ServletContext sc
String path = sc.getRealPath(...)
EDIT: Can I do something like this in myServlet
servlet?:
String realPath = sc.getRealPath("/WEB-INF/myFile");
But then how do I pass this realPath
variable into myClass
so I can use it there instead of in myServlet
?
java jsp session servlets realpath
java jsp session servlets realpath
edited Nov 15 '18 at 4:09
James McTyre
asked Nov 15 '18 at 4:01
James McTyreJames McTyre
535
535
why are your extendingpublic class myClass extends HttpServlet
?
– Scary Wombat
Nov 15 '18 at 4:14
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18
add a comment |
why are your extendingpublic class myClass extends HttpServlet
?
– Scary Wombat
Nov 15 '18 at 4:14
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18
why are your extending
public class myClass extends HttpServlet
?– Scary Wombat
Nov 15 '18 at 4:14
why are your extending
public class myClass extends HttpServlet
?– Scary Wombat
Nov 15 '18 at 4:14
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18
add a comment |
1 Answer
1
active
oldest
votes
create a class
public class MyClass {....}
Have a variable of type
ServletContext
private ServletContext myContext;
Set value through Constructor or setter
void setContext (ServletContext sc) {myContext = sc;}
Use it
myContext.get....("xxx");
Edit
You can use this class from your servlet as
doPost (....) {
....
ServletContext sc = session.getServletContext();
MyClass mc = new MyClass ();
mc.setContext (sc);
// now the context is **in** the MyClass instance - how you use it is up to you.
If I put my settervoid setcontext(...){...}
inmyClass
, where is thesc
coming from?
– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from yourServlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter formyContext
inmyServlet
?
– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
|
show 8 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312255%2freceiving-servletcontext-into-a-plain-java-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
create a class
public class MyClass {....}
Have a variable of type
ServletContext
private ServletContext myContext;
Set value through Constructor or setter
void setContext (ServletContext sc) {myContext = sc;}
Use it
myContext.get....("xxx");
Edit
You can use this class from your servlet as
doPost (....) {
....
ServletContext sc = session.getServletContext();
MyClass mc = new MyClass ();
mc.setContext (sc);
// now the context is **in** the MyClass instance - how you use it is up to you.
If I put my settervoid setcontext(...){...}
inmyClass
, where is thesc
coming from?
– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from yourServlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter formyContext
inmyServlet
?
– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
|
show 8 more comments
create a class
public class MyClass {....}
Have a variable of type
ServletContext
private ServletContext myContext;
Set value through Constructor or setter
void setContext (ServletContext sc) {myContext = sc;}
Use it
myContext.get....("xxx");
Edit
You can use this class from your servlet as
doPost (....) {
....
ServletContext sc = session.getServletContext();
MyClass mc = new MyClass ();
mc.setContext (sc);
// now the context is **in** the MyClass instance - how you use it is up to you.
If I put my settervoid setcontext(...){...}
inmyClass
, where is thesc
coming from?
– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from yourServlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter formyContext
inmyServlet
?
– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
|
show 8 more comments
create a class
public class MyClass {....}
Have a variable of type
ServletContext
private ServletContext myContext;
Set value through Constructor or setter
void setContext (ServletContext sc) {myContext = sc;}
Use it
myContext.get....("xxx");
Edit
You can use this class from your servlet as
doPost (....) {
....
ServletContext sc = session.getServletContext();
MyClass mc = new MyClass ();
mc.setContext (sc);
// now the context is **in** the MyClass instance - how you use it is up to you.
create a class
public class MyClass {....}
Have a variable of type
ServletContext
private ServletContext myContext;
Set value through Constructor or setter
void setContext (ServletContext sc) {myContext = sc;}
Use it
myContext.get....("xxx");
Edit
You can use this class from your servlet as
doPost (....) {
....
ServletContext sc = session.getServletContext();
MyClass mc = new MyClass ();
mc.setContext (sc);
// now the context is **in** the MyClass instance - how you use it is up to you.
edited Nov 15 '18 at 6:26
JimHawkins
2,98482241
2,98482241
answered Nov 15 '18 at 4:17
Scary WombatScary Wombat
35.3k32252
35.3k32252
If I put my settervoid setcontext(...){...}
inmyClass
, where is thesc
coming from?
– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from yourServlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter formyContext
inmyServlet
?
– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
|
show 8 more comments
If I put my settervoid setcontext(...){...}
inmyClass
, where is thesc
coming from?
– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from yourServlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter formyContext
inmyServlet
?
– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
If I put my setter
void setcontext(...){...}
in myClass
, where is the sc
coming from?– James McTyre
Nov 15 '18 at 4:26
If I put my setter
void setcontext(...){...}
in myClass
, where is the sc
coming from?– James McTyre
Nov 15 '18 at 4:26
You construct and call this code from your
Servlet
– Scary Wombat
Nov 15 '18 at 4:28
You construct and call this code from your
Servlet
– Scary Wombat
Nov 15 '18 at 4:28
I should code my setter and getter for
myContext
in myServlet
?– James McTyre
Nov 15 '18 at 4:30
I should code my setter and getter for
myContext
in myServlet
?– James McTyre
Nov 15 '18 at 4:30
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Huh? Which step 1-4 indicates that you should do that?
– Scary Wombat
Nov 15 '18 at 4:34
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
Your reply said so.
– James McTyre
Nov 15 '18 at 4:36
|
show 8 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312255%2freceiving-servletcontext-into-a-plain-java-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why are your extending
public class myClass extends HttpServlet
?– Scary Wombat
Nov 15 '18 at 4:14
The guy tutoring me told me to put it. When I asked him, he didn't really have an answer. Like I said, I just started learning servlets and JSP today
– James McTyre
Nov 15 '18 at 4:16
maybe a xy problem - what are you really trying to achieve?
– Scary Wombat
Nov 15 '18 at 4:18