2

I am trying to pass a Java String as a parameter for a JavaScript function. Yes I know Java and JavaScript are not the same, but I am using scriplets inside a .jsp.

I need a way to access the dynamically created java Scriplet object (componentTypePK) inside this JavaScript function. I thought the best way to do this would be to pass the Java Scriplet Object into the JavaScript function but I am getting the error described below.

This is my JavaScript Function:

<script LANGUAGE="JavaScript">
    function showFormsInUse(componentTypePk){
         window.open('/showFormsInUse.do?<%="documentTypePk="+request.getParameter("documentTypePk")+"programAreaPk="+request.getParameter("programAreaPk")+"ComponentTypePK="+componentTypePk');   
    }
</script>

Here is where I call the JavaScript function passing in a Java String as a parameter.

<% if(getOppList(context,componentType.getString("ComponentDef_pk")).length()>1){%>
    <TD CLASS="ListCode">
        <IMG onclick="showFormsInUse(<%=componentType.getString("ComponentDef_pk")%>)" SRC="images/check.gif" TITLE="<%=getOppList(context,componentType.getString("ComponentDef_pk"))%>" WIDTH="16" HEIGHT="15" BORDER="0">
    </TD>
<% }else{%>
    <TD CLASS="ListCode">
        <IMG SRC="images/check.gif" TITLE="<%=getOppList(context,componentType.getString("ComponentDef_pk"))%>" WIDTH="16" HEIGHT="15" BORDER="0">
    </TD>
<%}%>

I would prefer not to initialize a Java string before my script and then pass that String into a Javascript variable this. Also I know Java Scriplets are bad form, so I would love to know if I could use JSTL to fix the problem.

Update: When I try to run and compile this code I get the following:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 77 in the jsp file: /documentForms.jsp
Invalid character constant
74: 
75: <script LANGUAGE="JavaScript">
76:     function showFormsInUse(componentTypePk){
77:          window.open('/showFormsInUse.do?<%="documentTypePk="+request.getParameter("documentTypePk")+"programAreaPk="+request.getParameter("programAreaPk")+"ComponentTypePK="+componentTypePk');   
78:     }
79: </script>

To summarize, is there any way to pass a Java Object into a JavaScript function?

Community
  • 1
  • 1
Turtle
  • 1,369
  • 1
  • 17
  • 32

2 Answers2

1

String ComponentTypePKJS isn't valid JS, I'm not even sure what you're trying to do here. I'm not sure why you wouldn't expose the request parameters as normal JSP properties accessible by EL, roughly:

window.open('/showFormsInUse.do?documentTypePK=${documentTypePk}&programAreaPk=${programAreaPk}&ComponentTypePk=' + componentTypePk);

Mind that you'll need to make sure everything is properly escaped, but you'd need to do that no matter what.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I updated the question to hopefully make it more clear – Turtle Aug 14 '15 at 17:24
  • @Turtle You can't pass a Java *object* into a JS function; Java is server-side. You can pass the string representation of an object into a JS function by doing what I said, since you should only be hitting the JSP after running through a servlet/controller/whatever anyway. You can build up the JS code that'll be sent to the client however you want, through heinous scriptlets or JSP EL, technically it doesn't matter, although scriptlets are basically evil. – Dave Newton Aug 14 '15 at 17:30
1

On the line 77 you have opened a scriplet tag but I dint see the closing tag for the same.

'/showFormsInUse.do?<%="documentTypePk="+request.getParameter("documentTypePk")+"programAreaPk="+request.getParameter("programAreaPk")+"ComponentTypePK= %>"+componentTypePk'

I would also suggest you to keep all the scriplet tags in jsp and have a clean js code.

<% if(getOppList(context,componentType.getString("ComponentDef_pk")).length()>1){%>
<TD CLASS="ListCode">
    <IMG onclick="showFormsInUse(<%=request.getParameter("documentTypePk")%>,<%=request.getParameter("programAreaPk")%>,<%=componentType.getString("ComponentDef_pk")%>)" SRC="images/check.gif" TITLE="<%=getOppList(context,componentType.getString("ComponentDef_pk"))%>" WIDTH="16" HEIGHT="15" BORDER="0">
</TD>

Js

<script LANGUAGE="JavaScript">
function showFormsInUse(documentTypePk,programAreaPk,componentTypePk){
     window.open('/showFormsInUse.do?documentTypePk='+documentTypePk+'programAreaPk='+programAreaPk+'ComponentTypePK='+componentTypePk);   
}

Vincent
  • 141
  • 5