I figured out how to grab the ServletContext on the java end looking at this thread, but I still am unsure how to grab it in the JS. I was looking at this explanation for the JS. Do I have to pass the ServletContext as a request object (can I see an example) or could I just access it in the JS? Any help much appreciated.
-
Hover the `jsp` tag, see the popbox, click `info` and scroll to near the bottom and read the **JavaScript** section. Yes, just let JSP print it as if it is JS code. – BalusC Mar 25 '11 at 15:33
-
Ok thanks, I look into how to make sure its secure doing it this way (which was why I was thinking it was bad practice). – B.Z.B Mar 25 '11 at 18:40
-
@BalusC.... Ha I feel silly, sorry to badger you with questions. Ok I think I get what I need to do for the JS side, thanks to all for your comments, made me realize how I can handle this. Essentially the difference between what the server sees and what the user's see was what I wasn't wrapping my mind around. With the framework I'm using in Ext-JS I can make a Ajax Request to the java side to grab the correct byte[] (based on some req params) and then pass it back to the JS and create an image out of it. – B.Z.B Mar 25 '11 at 20:49
4 Answers
If you're talking about JavaScript as part of web pages, you can't access it at all. That JavaScript is being executed on the browser after the page loads. At this point, the ServletContext is long gone.
What you probably want to do is generate JavaScript based on the ServletContext. This is simply a matter of printing what you want in the JavaScript as constants that are rendered in to the page via the JSP that creates it, just like for HTML etc. At this point it's not JavaScript, it's simply text like anything else in the JSP page.

- 115,893
- 19
- 128
- 203
-
I need to access a HashMap that I made with ArrayList
values. I don't think just reading it into a constant would work here, and on top of that doing it this way I thought was bad practice. – B.Z.B Mar 25 '11 at 15:50 -
-
hm, well the overall idea behind this is to query the DB for some image blobs (utilizing the arrayList
for grouping purposes) and store it within the servletcontext, then when I want to display the images on the server I would just need to grab the the correct arrayList and display the blobs contained within. We're getting the images from the DB rather then a web publisher for scaling purposes, and the images would be dynamic. – B.Z.B Mar 25 '11 at 18:32 -
You can't "display objects on the server", only on the browser. The server doesn't display anything. You can embed images in your page using a data url (I wouldn't recommend it), or the page can make a normal request to your server for the image data using the img tag, which can then be streamed from your ServletContext cache with a custom Servlet handler responding to the img tag requests. – Will Hartung Mar 25 '11 at 18:46
-
I'll have to look more in on that second option, that seems to be what I'll need to do. – B.Z.B Mar 25 '11 at 18:52
Unless you are doing server-side javascript you cannot get a handle to the ServletContext because it simply doesn't exist in the browser. What you can do, though, is to read required attributes from ServletContext and create dynamic Javascript in your JSP/Servlet etc.

- 11,385
- 7
- 42
- 69
Application implicit object is an instance of javax.servlet.ServletContext. It is basically used for getting initialization parameters and for sharing the attributes & their values across the entire JSP application, which means any attribute set by application implicit object would be available to all the JSP pages. in our JSP page, we used below in javascript function enclosed by tag
<script type="text/javascript">
somefunction(){
var somevar = <%=application.getAttribute("attributeName")>
}
</script>

- 1,698
- 1
- 17
- 20
JS runs on the client side from within browser. Java objects available in the container can be accessed from within the Java code written in JSP/Servlets.
ServletContext object cannot be directly accessed from within JS - simply because the process and the context in which JS runs is different from the VM/Context in which ServletContext object exists.
From your question it appears that you want to access object data accessible to JS - you can achieve this by using JS-Java bridge like DWR (Direct Web Remoting) - please have a look at the link - http://directwebremoting.org/dwr/index.html

- 17,065
- 2
- 26
- 22