Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Variable with struts to javascript

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 ;
}
like image 586
Darkcloud Avatar asked Aug 23 '26 15:08

Darkcloud


2 Answers

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.

like image 115
Andrea Ligios Avatar answered Aug 26 '26 04:08

Andrea Ligios


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,

like image 30
albciff Avatar answered Aug 26 '26 05:08

albciff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!