3

I have a big problem passing a String Variable to a javascript function that creates an applet.

In my Java class i'm passing the variable "endpoint" throught the request

ActionContext.getActionContext().getRequest().setAttribute("endpoint",endpoint);

But I'm unable to recover it in the jsp page. I have tried with several html and struts elements but I can't get it in the js function. The code in my js function (included in the jsp) for recover the variable is:

var endpoint = document.getElementById('endpoint');

Hope that you could lend me a hand. Thank you very much in advance.

Edit: The code of the js function is:

function createApplet(){
var content = '<APPLET id="applet" '+
         'code="com.usr.local.AppletFiles.class" '+         'archive="applet/wsdl4j.jar,applet/xercesImpl.jar,applet/log4j.jar,applet/commons-logging.jar,applet/axis-ant.jar,applet/axis-schema.jar,applet/commons-discovery.jar,applet/jaxrpc.jar,applet/axis.jar,applet/saaj.jar,applet/serializer.jar,applet/xalan.jar,applet/xml-apis.jar,applet/JarApplet.jar">'+
         ' width="0"  height="0"  mayscript="true"  >';

  var tableApplet = document.getElementById('tableApplet');

  var endpoint = document.getElementById('endpoint');
  alert('Endpoint: '+ endpoint);

  var param= '';

  for(i=0;i<tableApplet .rows.length;i++){

    content += '<PARAM NAME="'+tableApplet .rows[i].cells[0].innerHTML+'" VALUE="'+tableApplet .rows[i].cells[1].innerHTML+'">';

    param+= tableApplet .rows[i].cells[0].innerHTML+'#';

  }

content += '<param name="parametros" value="'+nombreParametros+'">';

content += '<param name="endpoint" value="'+endpoint+'">';

content +='&lt;/APPLET&gt;';

alert('content ' + content );

return content ;
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Darkcloud
  • 33
  • 1
  • 7
  • Show the code how do you create an applet using javascript. – Roman C Feb 27 '14 at 09:03
  • Welcome to StackOverflow! You cannot read request attributes from javascript. Have a look here: http://stackoverflow.com/questions/15238647/passing-a-java-variable-to-a-javascript-function – Angelo Fuchs Feb 27 '14 at 09:43
  • Thank you Angelo. ¿Could I pass the parameter "endpoint" to the html code included in the jsp and after that recover it from the html code? – Darkcloud Feb 27 '14 at 09:56
  • @user1853334: if the solution below works, please remember to accept it – Andrea Ligios Feb 27 '14 at 12:31

2 Answers2

2

Use OGNL to inject the value of the request attribute in the JavaScript code:

var endpoint = '<s:property value="%{#request.endpoint}" />';

Also take a look at OGNL Basics, especially the last table in the page.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
1

You can use jstl tag (http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm) and put the request attribute on javascript var, you must do something similar to:

In your jsp include:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

And then in the jsp:

<script type="text/javascript">

var endpoint = '<c:out value="${pageContext.request.endpoint}"/>';

</script>

When jsp is evaluated on server before render in client browser so you will have in endpoint global js variable the request attibute value, and then you can use it on your js code.

however I think is possible that you are using an applet for a wrong purpose for various reasons:

  • I see too many jars on archive parameter, take in account that before each client want to run your applet all these jars must be downloaded by the JVM plugin to the client machine, in your case this could be too much time. Also probably if you want to invoke an endpoint you need to sign all the jars with an accepted certificate issued by a java recognized CA.

  • Looks like you want that applet invokes some remote service... why? if it's not extremely necessary why don't you do from your server?.

Hope this help,

albciff
  • 18,112
  • 4
  • 64
  • 89