Java code based on a javascript variable

Hi,
Based on click of a either linkA or linkB I set a javscript variable (say vA or vB).
After this I reload the browser.
My page on the browser in a jsp which gets texts from a properties file (say prop.properties).
But now based on vA or vB I wish to load vAprop.properties or vBprop.properties.
I know I cannot access javascript variables in Java code. Please let me know a work around for this.
I need it very badly.
Thanks in advance.

Have your javaScript open the link with a parameter
yourFile.jsp?param=A
yourFile.jsp?param=B
in your JSP do a
request.getParameter("param");
You can also use javaScript to post the values if you dont want them to appear in the URL.

Similar Messages

  • Assigning a java value(filepath) to a javascript variable

    I have a JSP where I have a java variable(obtained from session) which is nothing but a filepath(say C:\superb.xpdl). When I try to use the variable in javascript using var scriptVar = "<%= sessionVar %>";, the sessionVar value i.e. the filepath is getting modified (like C:superb.xpdl). Slashes are interpredted in a different way(\t for tab, \n for new line etc.,) How to get the filepath with no modifications while assigning to javascript variable? will encoding(using escape()) and decoding help?

    unfortunately you need to double up the slashes
    maybe this will work
    scriptVar = "<%= sessionVar.replaceAll("\\", "\\\\") %>";
    or maybe it will cause a infinite recursion problem (i'm too lazy to try it)

  • How to execute a java code ...

    how to execute a java code just like in javascript:
    for example in javascript: we can create a function :
    name_fonction=new Function("param1","param2",..., "Instructions");
    but can it be supported in the common java code? how to do it?
    Je suis a little urgent...
    Thanks a lot!!!

    See beanshell
    http://www.beanshell.org/
    which should be include in the next J2SE 6.0 or 7.0.
    See JSR 274 at http://jcp.org/en/jsr/detail?id=274
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                                                                                                                                                               

  • How to pass a JavaScript variable into a java method

    I would like to know how to pass a JavaScript variable into a java method with in a <% %> tag inside a JSP file like so:
    <%@ page contentType="text/html;charset=windows-1252"%>
    <html>
    <head>
    <script LANGUAGE="JavaScript">
    myValue = someDynamicValue;
    <% System.out.println(myValue)%>
    </script>
    </head>
    <body>
    </body>
    </html>
    obviously "System.out.println(myValue)" will not work because myValue is seen as a java variable and not a JavaScript variable.
    I would like to know how to let the jsp file, that I wrote in the above code, see myValue as a JavaScript variable and not a java variable so that I can pass it to a java method.
    NOTE: the java method does not have to be a println() method, it can be any method of my choice.
    NOTE: someDynamicValue is a JavaScript value that can dynamically change

    I don't believe you can. JSPs are really just elaborate templates that an engine such as Tomcat parses and generates an HTML page based on. That page is then displayed to the user. By the time you want to use some function in Javascript, the JSP has already been parsed and generated.
    Basically, Javascript and JSPs can't talk to each other. One's server-side and the other is client-side.

  • Calling a javascript function from java code and getting tha value in Java

    Hi,
    I would like to call a Java script function confirmRemove() from Java code upon meeting a condition..
    for example the code snippet is:
    if(true){
    // I want to call js confirmRemove() over here. And get the value of variable "answer" in this if block.
    <html>
    <head>
    <script type="text/javascript">
    function confirmRemove() {
         var answer = confirm("Are you sure you want to Delete?")
    </script>
    </head>
    <body>
    <form>...

    Hi,
    Back in 2003 I have used an Applet which contain java code and this java code was calling the java scripts ( different methods, DHTML etc..)
    There was a component developed by NetScape called JSObject I am not sure it there is other third party component other then the JSObject
    look at this article which shows how (based on JSObject)
    [http://java.sun.com/products/plugin/1.3/docs/jsobject.html|http://java.sun.com/products/plugin/1.3/docs/jsobject.html]
    Regards,
    Alan Meio
    London,UK

  • Passing struts-tag value to java / javascript variable in JSP

    Hello all,
    Im trying to pass the value got from the struts-tag to the java variable in the Jsp. However Im not able to get the o/p. Can u guys help me on this pls...
    <logic:notEmpty name="TreeBean" property="list">
        <logic:iterate id="Type" name="TreeBean" property="list" indexId="i">
         <br/><bean:write name="Type" />
        </logic:iterate>
    </logic:notEmpty>Here the list can be populated in the jsp page. However i want the values into jsp list. Im not able to populate the same..
    <% List jspList = null;
    %> I want the values into the jspList variable for further processing. Can u guz pls help me on this....

    Which do you want it in?
    A java variable for use in <% scriptlet code %> or a javascript variable for use on the client end?
    scriptlet:
    <jsp:useBean id="TreeBean" type="java.util.List" class="java.util.ArrayList" scope="?????" />
    Having scriptlet code on the page is considered bad coding style.
    What is it you are trying to accomplish by making the value accessible?
    What are you trying to do in java / javascript that can't be done with JSP tags?

  • How to access a javascript variable from Java?

    Here is my code:
    function validateLoginForm() {
        var username = document.getElementById('un');
        setCookie('un', username, 3650);
        //etc.
    <%   
        HttpSession httpSession = request.getSession();
        httpSession.setMaxInactiveInterval(30 * 60); //30 minutes
        httpSession.setAttribute("un", username); //!prob here - cannot resolve 'username'
    %>
    }...but how do I access the javascript variable 'username' from the Java code?
    Thanks,
    James

    The only way to pass values between JavaScript and JSP is through cookies. It sucks, I know, but right now that is the only option.
    You already are creating a cookie in JavaScript. So go ahead and read it in Java:
    Cookie[] cookies = request.getCookies();
    for(int i = 0; i < cookies.length; i++) {
        Cookie c = cookies;
    if (c.getName().equals("un")) {
    // Do what you need here.

  • Java Applet won't load when a JavaScript variable "this.Document" is used.

    Hi,
    Creating a "this.Document" variable within JavaScript interferes with some versions of Java. So far it breaks on the following versions, using IE7 on Windows XP:
    * JRE 6.0 Update 7
    * JRE 6.0 Update 6
    * JRE 6.0 Update 5
    * JRE 6.0 Update 4
    [Due to time constraints JRE 6.0 to JRE 6.0 Update 3 were not tested]
    * JRE 5.0 Update 22
    [Due to time constraints JRE 5.0 Update 16 to JRE 5.0 Update 21 were not tested]
    * JRE 5.0 Update 15
    [Due to time constraints JRE 5.0 Update 1 to JRE 5.0 Update 14 were not tested]
    * JRE 5.0
    The following HTML demonstrates this problem when you have any of the above versions of Java installed:
    <html>
        <head>
            <title>Java Bug</title>
            <script type="text/javascript" language="javascript">
                this.Document = 'string'; // Remove this line to get it to work again
            </script>
        </head>
        <body style="background-color:black">
            <applet id="testVM" alt="Something is wrong. Java is not working."
                codebase="http://www.java.com/applet" code="testJava2_1/TestVMApplet"
                archive="TestVM2-test.jar" width="500" height="280">
                <param name="locale" value="en" />   
                <param name="titleSize" value="22" />
                <param name="subtitleSize" value="18" />   
            </applet>
        </body>
    </html>The page only displays a white box where the Java Applet should be. If you then remove the "this.Document" variable or simply rename it to something else then the applet loads without problems. Normally I would just use a different variable name but the variable name is placed within the Mootools JavaScript Framework and is not easily changed without forking my own project.
    Does anyone know why this happens and if it can be fixed without having to update to the latest version of Java?
    Thanks

    EJP wrote:
    'this.document' is a predefined variable that defines the URL source of the current page. If you are referring to the predefined variable that provides access to the Document Object Model then I believe you are mistaken, that is 'window.document' or just 'document'.
    EJP wrote:
    Your name either conflicts with that or is getting confused with it somewhere.The 'document' variable is a parent of the window object e.g. 'window.document' but my variable has the context of 'this'. That means 'this.document.URL' returns 'undefined' instead of the URL for the current page.
    JavaScript is also case sensitive so 'document.URL' is not the same as 'Document.URL', the last of which returns 'undefined'.
    If Java relealy was confusing my variable with the predfined one then I would expect the same behaviour if I were to use differnt variations of the variable. However the follwoing JavaScript doesnt affect the Applet in anyway:
    <script type="text/javascript" language="javascript">
        //this.Document = 'string'; // Remove this line to get it to work again
        this.document = 'string';
        document = 'string';
        Document = 'string';            
    </script>For some strange reason it only affects the Applet when 'this.Document' is used. Why would Java be affected in this way in the first instance? Even setting the 'scriptable' parameter to false doesn’t help:
    <param name="scriptable" value="false">Are there any other settings I am not aware of that could maybe prevent Java from loooking at the JavaScript?
    EJP wrote:
    Use another name.As I mentioned in my original post this is not easily done, I can edit the source code on my own site but the code still remains unchanged on the Mootools JavaScript Framework website.
    Thanks

  • Can i write java code in JavaScript function

    I want to call JavaBeans within JavaScript function. Can I do it?
    I have a button in JSP . When the button be clicked,the data be inputted will be checked by JavaScript function. If check is ok, then call a Bean's function to write the data to database.
    Please Help!

    well indeed u can write java code in javascript functions. But probably it will not work the way u want it to.
    when u say that u click a button, it means that some client side action is performed. Now if u wud require a java bean to be called then it means that the server needs to be contacted for the same, and if that has to be done, necessarily the form needs to be submitted.
    U can populate values from a java bean and then the same can be made available to a javascript variable / function when the page is loaded rather than, when the button is clicked.
    What u can do is on click of button u can process [display / calulate..etc ] the information which has already been got by the java bean.
    Kris

  • Using a Variable from web-inf/web.xml in java code?

    I would like to create a read-only variable with a value in the /web-inf/web.xml file and then access that value within a java service method within the Application Module.
    Where / how do I read the value from the web.xml file and make it accessible within my java code?
    thanks

    Hi,
    you can define context parameters in web.xml that you can access from Java in the web application
    The following is a JSF examle that checks a context parameter in a PhaseListener, where the string is stored ina Java constant (could be "myParam")
    ExternalContext exctx = FacesContext.getCurrentInstance().getExternalContext();
    useSessionPageCache = exctx.getInitParameter(Constants.USE_PAGE_CACHE) == null ? false :
    (exctx.getInitParameter(Constants.USE_PAGE_CACHE)).equalsIgnoreCase("TRUE") ? true : false;
    From here you can call a method on the AM to set the value or look for another option.
    Frank

  • Assigning javascript variable to java variable??

    I'm working on a web app, and part of it is form to add/update customers information. What my problem is I have all the cities of the US and Canada in a database and was using javascript to load the cities depending on which province/state is selected from another list box. Because the database is so large it takes a long time and everytime the page loads it takes a long time because the java code runs. The cities are loaded into the page by a javabean which returns a vector with all of the cities. What I want to do is use the province or state id to pass to the bean and just retreive the cites for that state as oppose to loading all of them. However, I'm stuck with assigning that value from my list box, which I would use javascript to determine, then passing it as a parameter to the bean.
    From the research I did, it seems like it can't be done. I'm seeking some advice thoough, as there would be someone who had this problem before and solved it or found a work around. Would be grateful for any help and advice. below is a part of my code that's presenting the obstacle.
    <script type="text/javascript">
    <!--
    function fillCityList()
         var num=document.mDetails.provinceId.options[document.mDetails.provinceId.selectedIndex].value
         var boxlength=0
         document.mDetails.cityId.options.length = 0     
         <%     
         for ( int ctr=0;ctr<cityList.size();ctr++)
              City aCity = (City) cityList.elementAt(ctr); %>     
              if(num==<%= aCity.getProvinceId() %>)
                        document.mDetails.cityId.options[document.mDetails.cityId.options.length]=new Option("<%= aCity.getCityName() %>", "<%= aCity.getCityId() %>");     
         <% } %>           
    // -->
    </script>
    </html>

    How about doing this:
    <html>
    <head>
    <script language="JavaScript">
    vfnStateChange = function() {
         var oState = document.forms['myform'].state;
         var sState = oState.options[oState.selectedIndex].value;
         document.location.href='thispage.jsp?state=' + sState;
    </script>
    </head>
    <body>
    <form name="myform" action="somepage.jsp">
    <select name="state" onchange="vfnStateChange();">
    <%
    ArrayList stateList = somebean.getStates();
    for(int i = 0; i < stateList.size(); ctr++) {
         State s = (State)stateList.elementAt(i);
    %>
    <option value="<%= s.getStateId() %>"><%= s.getStateName() %></option>
    <% } %>
    </select>
    <select name="city">
    <%
    String state = request.getParameter("state");
    if(state == null) {
    %>
    <option value=""></option>
    <%
    } else {
         ArrayList cityList = somebean.getCities(state);
         for(int i = 0; i < cityList.size(); i++) {
              City c = (City)cityList.elementAt(i);
    %>
    <option value="<%= c.getCityId() %>" <%= c.getStateId().equals(state) ? "" : "selected=\"selected\"" %>><%= c.getCityName() %></option>
    <%
    %>
    </select>
    </form>
    </body>
    </html>

  • How to use a variable of jsp custom tag in my java code?

    hi folks,
    i got a folderList tag like this:
    ArrayList list = new ArrayList(20);
    %>
    <foo:folderList path="${attributes.newsPath}" var="year" contentType="folder">
    <%
              list.add(${year.contentEntryName});   // of course, this doesn't work
    %>
    </foo:folderList>I think, its clear what I want to do. I am just wondering how I can use my Variable "year" in the Java code between <% %>...

    <% list.add(year.getContentEntryName()); %>
    You have to have defined in the custom tag that "var" relates to a scripting variable that you want defined.
    <tag>
      <variable>
        <name-given>var</name-given>
        <variable-class>myPackage.myClass</variable-class>
        <declare>true</declare>
        <scope>NESTED</scope>
      </variable>
    </tag> Cheers,
    evnafets

  • Regarding use of java code inside javascript

    Hi ,
    I have doubt regarding how to use java code inside javascript,
    i am giving my application code here
    <%@page import="java.util.*"%>
    <%@ page import="com.suuny.sard.pmt.pat.*"%>
    <SCRIPT LANGUAGE="JavaScript">
    function fun(){
         var ac=document.login.Projectname.options[document.login.Projectname.selectedIndex].value;
         var c = document.login.task1.value;
         alert(ac);
         alert(c);
         return true;
    </SCRIPT>
    <form name="login" action="/pmt/jsp/Leavemanagement/Leave_Ems_HomePage.jsp" method="get" encType="x-www-form-encoded" >
    <table width="100%" Align="Center" cellspacing="0" cellpadding="0" border="0">
    <tr class="tablerow"><td colspan="6" align="center"><font class="pagehead" align="center">Create Tasks</font></td></tr>
         <tr class="tableheader">
              <th>Project
              <th>Task-Id
              <th>TaskName
              <th>Assigned To
              <th>description
              <th>Milestone
         </tr>
         <%
              for(int i=1;i<2;i++){%>
                   <tr class="tablerow"><td align="center">
                   <select class="SELECT" NAME="Projectname" VALUE=" " onchange="return fun();" >
                   <!--<option value=""> </option>-->
                   <%
                        ArrayList ar =null;
                        ArrayList projectids=null;
                        Hashtable ht = new Hashtable();
                        ProjectNames pnames=new ProjectNames();
                   ht = pnames.getProjectNames();
                        ar=(ArrayList)ht.get("projects");
                        projectids=(ArrayList)ht.get("projectIds");
                        for(int j=0;j<ar.size();j++){
                        out.println("<option value=\""+projectids.get(j)+"\">"+ar.get(j)+"</option>");
                   %>
    </select>
         <%
                   out.println("<td align=\"center\"><input type=\"text\" name=\"task"+i+"\"value=\"\" size=\"25\"></td>");
                   out.println("<td align=\"center\"><input type=\"text\" name=\"taskname"+i+"\"value=\"\" size=\"25\"></td>");
              %>
              <td align="center">
                             <select class="SELECT" NAME="Projectname1" VALUE=" ">
                             <!--<option value=""> </option>-->
                             <%
                                  ArrayList ar1 =null;
                             ar1 = pnames.getProjectResource("VXML02");
                                  for(int j=0;j<ar1.size();j++){
                                  out.println("<option value=\""+ar1.get(j)+"\">"+ar1.get(j)+"</option>");
                             %>
    </select>
              <%
                   out.println("<td align=\"center\"><input type=\"text\" name=\"description"+i+"\"value=\"\" size=\"25\"></td>");
                   out.println("<td align=\"center\"><input type=\"text\" name=\"milestone"+i+"\"value=\"\" size=\"25\"></td><tr>");
              out.println("<tr class=\"tablerow\"><td colspan=\"6\" align=\"center\"><input class=\"txtbuttonsmall\" type=\"submit\" value=\"save\"></td>");
         %>
    </Table>
    </form>
    My question is :
    I selected one project from first combo box ,i need project members corresponding to the project.
    In my code "pnames.getProjectResource("VXML02");" in this function iam passing one static projrct value,but i need dynamically selected project from first combo box(<select class="SELECT" NAME="Projectname" VALUE=" " onchange="return fun();" >),so please help me guys.

    Hi Marco,
    I "executed" some java code within XSLs file. I have quoted the word executed because I didn't really run java code, but I used a simply trick that I describe you below.
    Into your XSLs put an iframe which is hided (it has an height of 0 pixel). As src of the iframe put the address of a portal component which execute your code (in my case calculate some PCD URL of some pages into a defined role). As result of component execution, I use the response.write method in order to execute some jscript code, which is able to interact with the HTML generated by XSLs files, for example in my case response.write put the PCD URL into a drop down list placed into the "edit" form.
    This works fine. I don't know if is suitable also for your case, anyway could be a hint, but pay attention to Roland's recommendations.
    Ciao
    Roberto

  • How to embid java code in JavaScript

    Hi!!
    I am writing JSP program.
    Do u know how to embid java code in javascirpt
    ex:
    <SCRIPT>
    alert("hello");
    <%
    System.out.println("Hello...Satish. ");
    %>
    </SCRIPT>
    This alert message is not comming when i add the java code.

    Do u know how to embid java code in javascirptYou can't.
    Java/JSP code runs, and produces a web page. At this point any html/javascript is treated just as "text" to output.
    That web page is then sent to the browser which runs javascript.
    All java code has been run/removed - maybe generating HTML in its place.
    The only way to call java code again is to submit an HTTP request - normally by clicking a link/submitting a form.

  • Java Code to trigger Custom Based Event in BO XI Release

    hi,
    I am new member of this community. I am very happy to be part of it. I am working in BO XI r2. I  need to schedule some reports based on custom event and I need to trigger this event externally using some java application. Is it possible to trigger custom event using java code??
    Need Help!.
    Regards,
    Yasar

    Hi, here is a sample.
    In future, please use for Java SDK related questions, you will get a quicker response here
    import com.crystaldecisions.sdk.exception.SDKException;
    import com.crystaldecisions.sdk.framework.CrystalEnterprise;
    import com.crystaldecisions.sdk.framework.IEnterpriseSession;
    import com.crystaldecisions.sdk.framework.ISessionMgr;
    import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
    import com.crystaldecisions.sdk.occa.infostore.IInfoStore;
    import com.crystaldecisions.sdk.plugin.desktop.event.IEvent;
    import com.crystaldecisions.sdk.plugin.desktop.event.IUserEvent;
    public class triggerEvent {
         public static void main(String[] args) {
              String lUser = "administrator";
              String lCMS = "servername";
              String lPWD = "";
              String lSecurity = "secEnterprise";
              IEnterpriseSession lEnterpriseSession = null;
              try {
                   //Retrieve the ISessionMgr object to perform the logon
                   ISessionMgr lSessionMgr = CrystalEnterprise.getSessionMgr();
                   //Logon to Enterprise
                   lEnterpriseSession = lSessionMgr.logon(lUser, lPWD, lCMS, lSecurity);
                   //Retrieve the InfoStore object from the Enterprise Session
                   IInfoStore lInfoStore = (IInfoStore)lEnterpriseSession.getService("", "InfoStore");
                   //Create the query to retrieve the all the events
                   String lQuery = "SELECT * " +
                                   "FROM   CI_SYSTEMOBJECTS " +
                                     "WHERE  SI_KIND = 'Event' " +
                                     "ORDER BY SI_NAME";
                   IInfoObjects lInfoObjects = lInfoStore.query(lQuery);
                   //Display a message if no results were returned
                   if (lInfoObjects.isEmpty())
                        return;
                   //Go through each event returned and add it to the table
                   for (int lIdx = 0; lIdx < lInfoObjects.size(); lIdx++)
                        //Retrieve the current event object
                        IEvent oEvent = (IEvent)lInfoObjects.get(lIdx);
                        IUserEvent lUserEvent = (IUserEvent) oEvent.getEventInterface();
                        lUserEvent.trigger();
                   lInfoStore.commit(lInfoObjects);
              } catch (SDKException e) {
                   e.printStackTrace();
              } finally {
                   if (lEnterpriseSession!= null) {
                        lEnterpriseSession.logoff();

Maybe you are looking for