How to use JSP tags in JavaScript file?
Is there any way?
JSP is a view technology which runs at the server side which you can use to write template text like HTML/CSS/JS in. You can use JSP taglibs and EL to control the page flow and output dynamically using Java code. This also concerns the JavaScript content/output. Once JSP has run, it basically produces a HTML page and sends it to the client side. You can use JSP to dynamically output back-end data as if it are JavaScript variables. For example:
<script>
var foo = '${someBean.someProperty}';
</script>
Once the HTML page (with CSS/JS inside) has arrived at the client side (rightclick page and view source, you won't see any line of Java/JSP code), then the HTML will start to be interpreted, the CSS will start to be applied and the JS will start to be executed. There's no means of Java/JSP code at the client side. If you view the generated source in the client, the above example would now look like:
<script>
var foo = 'somePropertyValue';
</script>
This way JavaScript has just instant access to server-side variables.
Now the other way round; the only way to let JavaScript access/invoke Java/JSP code is to actually send a HTTP request to the server side. This can be done several ways: doing a window.location
to do a synchronous GET request, or doing a form.submit()
to do a synchronous GET or POST request, or doing a XMLHttpRequest#send()
to do an asynchronous request (also known from Ajax).
Alternatively you can also just let JavaScript set a (hidden) input field of a form so that it "automatically" get taken with the form submit whenever the user submits the form. Either way, the Java/JSP code at the server side will then be able to access JavaScript-controlled values the usual request parameter way.
To learn more about the wall between Java/JSP and JavaScript you may find this article useful.
I have wondered about this when I have values in my session or context I wanted exposed in the client side. I create a jsp file with javascript mime type which just include global variable values. Which I then include at the top of my page and reuse the values where necessary.
for eg:
**globalVar.jsp**
var ctxPath = "<%=request.getContextPath()%>";
**script.js**
ajaxURL = ctxPath + "/path/to/call?param=value";
You can even namespace this as outlined here
Yes, you can use JSP to generate JavaScript to send to the browser. Just point the <script>
tag to a URL that leads to a JSP page that sets the MIME type of the response to "text/javascript".
No, you cannot use JSP tags in JavaScript in the browser. JSP is a server-side technology, which means that processing must be done on the server.
You can easily use java tags to assign to a java variable which is later invoked inside the javascript code.
<% String name="Peter" %>
and then in javascript..
<script type="text/javascript">
var _name = <%= name %>
@BalusC answer explain's well what is server-side and client-side programming. However I would like to highlight a point that if you really want to get run jsp
within your javascript
, then you can give it file extension .jsp
instead of .js
because what determines whether a file is a javascript
file or not is what MIME media type. You can set MIME from JSP using:
<%@ page contentType="text/javascript" %>
and now you can use jsp
within your javascript
like:
var someMessage = "${someMessage}"
var anotherMessage = "${anotherMessage}"/>"
and now, you can link directly to it as:
<script type="text/javascript" src="/script.jsp"></script>
精彩评论