How to use scriptlet inside javascript

Can someone check this example and share the results? http://timothypowell.net/blog/?p=23
When I do this:

var myVar = '<% request.getContextPath(); %>'; alert(myVar); 

I get: '<% request.getContextPath(); %>'. '<% request.getContextPath(); %>'.

Removing closed single quotes from '<% request.getContextPath (); %> '; gives a syntax error. How to use script or expression inside js function?

EDIT: this link has an explanation that helped me:
http://www.codingforums.com/showthread.php?t=172082

+8
javascript jsp
source share
4 answers

It looks like you are posting JSP code in a JavaScript page, or at least on a non-JSP page. Scripts can only be included on a JSP page (usually this is a * .jsp parameter).

An assertion submitted if handled by the JSP compiler will cause myVar to be equal to '' as the script format that you use <% ...%> will execute Java code between tags but will not return the result.

So, to use this tag, you need to manually write the value to the output stream of the request. 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) 

For all that, scenarios in most cases are seen as bad practice. In most cases, you need to use JSTL expressions and custom tags.

+3
source share

This line of code should be placed in the HTML <script> in the .jsp file. In this way, JspServlet process scriptlets (and other special JSP / EL expressions).

 <script>var myVar = '<%= request.getContextPath() %>';</script> 

Note that <%= %> is the correct syntax for printing a variable, <% %> does not.

Or, if it is intended for servicing in a standalone .js file, you need to rename it to .jsp and add the following to the top of the file (and change the URL <script src> accordingly):

 <%@page contentType="text/javascript" %> ... var myVar = '<%= request.getContextPath() %>'; 

Thus, the JspServlet process it, and the browser will be prompted to interpret the body of the JSP response as JavaScript instead of HTML.


Not related to a specific problem, note that scripting is considered bad practice . Use EL.

 var myVar = '${pageContext.request.contextPath}'; 
+5
source share

You cannot run a scriptlet inside javascript by specifying its normal extension .js . However, you can provide the .js file extension .jsp , and then a link to it as:

 <script type="text/javascript" src="/script.jsp"></script> 

and now you can use jsp in your javascript , for example:

 var someMessage = "${someMessage}" var anotherMessage = "${anotherMessage}"/>" 

This action is fully valid because what determines whether the file is a javascript file or not is a type of MIME medium. You can install MIME from JSP using:

 <%@ page contentType="text/javascript" %> 
+1
source share
 var myVar = '<%=request.getContextPath() %>'; alert(myVar); 

You forgot to leave put = before the request and delete; after getContextPath ()

0
source share

All Articles