admin管理员组文章数量:1204006
Can someone test this example and share the results?
/?p=23
When I do:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
I get : '<% request.getContextPath(); %>'.
Removing the enclosing single quotes from '<% request.getContextPath(); %>'; gives syntax error. How can I use the scrptlet or expresion inside a js function?
EDIT: this link has an explanation that helped me:
.php?t=172082
Can someone test this example and share the results?
http://timothypowell.net/blog/?p=23
When I do:
var myVar = '<% request.getContextPath(); %>';
alert(myVar);
I get : '<% request.getContextPath(); %>'.
Removing the enclosing single quotes from '<% request.getContextPath(); %>'; gives syntax error. How can I use the scrptlet or expresion inside a js function?
EDIT: this link has an explanation that helped me:
http://www.codingforums.com/showthread.php?t=172082
4 Answers
Reset to default 5That line of code has to be placed in a HTML <script>
tag in a .jsp
file. This way the JspServlet
will process the scriptlets (and other JSP/EL specific expressions).
<script>var myVar = '<%= request.getContextPath() %>';</script>
Note that <%= %>
is the right syntax to print a variable, the <% %>
doesn't do that.
Or if it is intended to be served in a standalone .js
file, then you need to rename it to .jsp
and add the following to top of the file (and change the <script src>
URL accordingly):
<%@page contentType="text/javascript" %>
...
var myVar = '<%= request.getContextPath() %>';
This way the JspServlet
will process it and the browser will be instructed to interpret the JSP response body as JavaScript instead of HTML.
Unrelated to the concrete problem, note that scriptlets are considered poor practice. Use EL.
var myVar = '${pageContext.request.contextPath}';
It sounds like you are placing the JSP code within a JavaScript page, or at least in a non-JSP page. Scriptlets can only be included in a JSP page (typically configured to be *.jsp).
The statement as presented, if processed by the JSP compiler, would result in myVar being equal to '' as the scriptlet format you are using <% ... %> executes Java code between the tags, but does not return a result.
So, to use this tag you would need to manually write a value to the request output stream. To get the desired functionality you need to do the following:
make sure your code is in a JSP page
use var myVar = '<%= request.getContextPath() %>'; (note the equals sign)
With all that said, scriptlets are viewed as bad practice in most cases. For most cases, your should be using JSTL expressions and custom tags.
You cannot run scriptlet
inside javascript
by giving it normal .js
extension. However you can give your .js
the file extension of .jsp
and then and link directly to it as:
<script type="text/javascript" src="/script.jsp"></script>
and now you can use jsp
within your javascript
like:
var someMessage = "${someMessage}"
var anotherMessage = "${anotherMessage}"/>"
This action is completely valid 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" %>
var myVar = '<%=request.getContextPath() %>';
alert(myVar);
You forgot to out put = before request and remove ; after getContextPath()
本文标签: jspHow to use scriptlet inside javascriptStack Overflow
版权声明:本文标题:jsp - How to use scriptlet inside javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738677688a2106360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.jsp
file and is your server configured to handle them? – casablanca Commented Mar 1, 2011 at 18:00var myVar = '<%= request.getContextPath(); %>';
– Linus Thiel Commented Mar 1, 2011 at 18:00