Passing javascript values to jsp without refreshing the page

Hi,
How do u pass a value of a javascript variable to the jsp without refreshing the page ?
For example, a file called test.jsp in which a javascript variable x contains value 254. I need to pass the value of x to a method declared in test.jsp(same page).

Hi Mona,
when i say refresh i do mean a blink of the browser.
This is a small example i wrote to show you how you can pass javascript varables to JSP variables. If you don't want the refresh to be seen by the user just include this code in a hidden frame on a page and instead of refreshing the entire page, refresh the hidden frame.
i have to say, i didn't test the code so i don't guarantee it's flawless.
if you need an more detailed example just tell me, i'll create one, but it won't be for today :)
<html>
<head>
     <title>Log in to the system</title>
</head>
<body>
<script>
//we retrieve the parameter 'user' from the URL
<%
  String username = request.getParameter("user");
%>
var user = "<%= username%>";
//check if there is a username in the URL
if(user=="null")
  //there is no username so we log the person in as a guest
  user="guest";
  //we refresh the page and set the user parameter to 'guest'
  //the parameter now contains the javascript variable 'user'.
  //The parameter can be read by the JSP (see the top op this script).
  //This way we gave the javascript variabele to the JSP variable
  location="login.jsp?user="+user;
else if(user=="guest")
{  alert("Welcome "+user+", I hope you like this site"); }
else
{  alert("Welcome "+user+", I'm glad to see you again"); }
</script>
</body>
</html>

Similar Messages

  • Passing JavaScript values to JSP variables

    Can any body correct the follwing code
    <Script language="JavaScript">
    function test( x )
    <%
    int num = x;
    num = num * 2;
    %>
    v.value = "<%out.print(num);%>";
    <input type="button" name="b" value="test" onClick="test(5)">
    <input type="text" name="v" value="0">
    In short, I am trying to pass JavaScript value to JSP variable. I hope that it is possible to do that. If it is possible then how can I do it. I want to assing the variable x passed to the JavaScript function called test to the JSP variable called num.
    Regards,
    Ageel

    Thank you for your reply,,,
    I think then the only way to do it is to post the
    value on the server and then use request.getParameter
    method in jsp code
    but the question now how can I post values to the
    server using JavaScript without reloading the pageyes... you can to it by create a new popup window which will submit the value to server after page was loaded... then, server return a value to the same window in html/jsp page which then using javascript to set it back to the opener and close up the window... however, this is not a good choice unless you have no other alternative...
    >
    There is other possible solution
    if I can get the text field value from the same page
    without reloading it that would work fine and will
    solve my problem, is it possible?yes... you can get the value from the textfield...
    for example :
    function showValueInTextField()
        alert(document.forms[0].elements["mytextfieldname"].value);
    >
    My final question> can jsp change things on the same
    page without reloading it. I mean can it work like
    JavaScript so that I can use it's internal functions
    instead of using java script :S
    not really know what you trying to say here...

  • How can we get a value in a drop down box without refreshing the page

    In my application i am having 14 drop down boxes. On selcting a particular value in a drop down box i am doing its corresponding functionality. I would like to get these values without refreshing the page each and every time i select a text box, Is it possible to get these values without refreshing the page each and every time.
    Raghu

    There is a new hype going on called AJAX. It is either that, or dumping a lot of information in javascript arrays and reading the information from there when you make selections. I would choose AJAX.

  • Passing JavaScript value to Jsp

    Hi,
    Any idea how to pass a value from javascript to jsp page when the page gets load on browser.
    I use below code but an event is require to pass the value. Is there any code that pass automatically the value from javascript to jsp.
    <html>
    <head>
    <title>Passing Javascript value to Jsp</title>
    <script type="text/javascript" language="javascript">
    var scriptVar = "Noy"
    var WinNetwork = new ActiveXObject("WScript.Network")
    document.write (WinNetwork.username)
    document.index.hiddenTextBox.value = WinNetwork.username
    </script>
    </head>
    <%
    String jspVar = null;
    if(request.getParameter("submit") != null){
         jspVar = request.getParameter("hiddenTextBox");
         out.println("Jsp Var : " + jspVar);
    %>
    <body>
    <form name="index" onSubmit="index.jsp" method="post">
    <input type="hidden" name="hiddenTextBox"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>
    Thanks,
    ~ukbasak

    JSP/Java runs at the server side, produces a HTML page, sends it to the client side and then stops running.
    HTML, containing under each CSS and JS, arrives at the client side and runs at the client side only.
    To invoke JSP/Java using HTML you need to fire a request to the server side. That can be either a plain vanilla <a> link, a <form> to be submitted or an asynchronous Ajaxical request.
    That said, the use of ActiveX in web applications is discouraged. It's a Microsoft IE proprietaty. With other words, it isn't and won't be supported by all alternative browsers which the other half of the world is using.

  • How can we update the date&time without refreshing the page

    Hi friends,
    I have a jsp page. In that there is Date and Time. The date and time should be updated for every one minute without refresh the page(dynamically).
    Its urgent for me
    Thanks
    Mallik

    hello friend ru looking at date and time @ client side frm the server side if it is all about the client side date i don't think there is use of AJAX triggers and rendering the view by modifying the dom...
    here is an example for you..
    <%@ page language="java"%>
    <HTML>
    <HEAD>
    <SCRIPT language="JavaScript">
    function startclock()
    var thetime=new Date();
    var nhours=thetime.getHours();
    var nmins=thetime.getMinutes();
    var nsecn=thetime.getSeconds();
    var nday=thetime.getDay();
    var nmonth=thetime.getMonth();
    var ntoday=thetime.getDate();
    var nyear=thetime.getYear();
    var AorP=" ";
    if (nhours>=12)
        AorP="P.M.";
    else
        AorP="A.M.";
    if (nhours>=13)
        nhours-=12;
    if (nhours==0)
       nhours=12;
    if (nsecn<10)
    nsecn="0"+nsecn;
    if (nmins<10)
    nmins="0"+nmins;
    if (nday==0)
      nday="Sunday";
    if (nday==1)
      nday="Monday";
    if (nday==2)
      nday="Tuesday";
    if (nday==3)
      nday="Wednesday";
    if (nday==4)
      nday="Thursday";
    if (nday==5)
      nday="Friday";
    if (nday==6)
      nday="Saturday";
    nmonth+=1;
    if (nyear<=99)
      nyear= "19"+nyear;
    if ((nyear>99) && (nyear<2000))
    nyear+=1900;
    this.document.getElementById("clockspot").innerHTML = "<b>"+nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear+"</b>";
    setTimeout('startclock()',1000);
    </SCRIPT>
    </HEAD>
    <BODY>
    <div id="clockspot"></div>
    <SCRIPT language="JavaScript">
    startclock();
    </SCRIPT>
    </BODY>
    </HTML>hope this might help :)
    REGARDS,
    RaHuL

  • Displaying the  results without refreshing the page

    Hi,
    I need to show the items added to the basket within a list box which is on the top frame without refreshing the page.the page is a jsp page.Could you please give me idea how this could be done.
    The problem is, there are 2 frames, one above and one below.In the below frame I select a product and add to the basket, at the same time I need to show these items in above frame within the list box, the above frame should not refresh, it should show the items as soon as the items are added.
    Thanks

    The cart bean is on the server? Then you will have to send a request to the server to get it added. How do you do that? You post a form. How do you post a form without submitting a page? You don't.
    You need to make a design decision. If you want all of your logic on the server, you will have to be content with client-side refreshes. If client-side refreshes and their inherent delays are not acceptable, you will have to learn to be savvy with JavaScript but realize that anything you do on the form is still only on the form until you submit it.

  • Is it possible to return a database query without refreshing the page?

    I have a form which has a drop-down select box. What I want to do is send the value from the select list to a CFC which runs some SQL code and returns a recordset. I then want to use this recordset on my original CFM page that has the form on it.
    Is it possible to do this without refreshing the page through some kind of AJAX? Apologies I am a newbie at AJAX but I did manage to write a bit of code that uses <cfdiv> to bind to a URL that does the database work and returns some HTML. But really I just want the database recordset and not sure how to get it?

    You could use AJAX, Flex Remoting or any such remoting technique. That is, if there is a case for it. However, from what you describe in the first paragraph, you don't need to. Just send the form to its own page.
    In the following example, the CFC and CFM page are both in the same directory under the root. I have made use of the cfdocexamples datasource which ships with ColdFusion.
    Employee.cfc
    <cfcomponent>
    <cffunction name="getEmployeeDetails"  output="false" returntype="query">
    <cfargument name="employee_id">
    <cfset var employeeDetails = queryNew("","")>
    <cfset var emp_id = arguments.employee_id>
    <cfif isNumeric(emp_id)>
        <cfquery name = "employeeDetails" dataSource = "cfdocexamples">
            SELECT firstname, email as email_name, department, phone, location
            FROM Employees
            WHERE emp_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#emp_id#">
        </cfquery>
    </cfif>
    <!--- Will activate error-handler in the caller --->
    <cfif NOT isNumeric(emp_id) or employeeDetails.recordcount EQ 0>
        <cfthrow message="You selected no employee.">
    </cfif>
    <cfreturn employeeDetails>
    </cffunction>
    </cfcomponent>
    testPage.cfm
    <cfif isDefined("form.employee_id")>
        <cfset employeeObj = createobject("component","Employee")>
        <!--- employeeDetails is a query--->
        <cfset employeeDetails = employeeObj.getEmployeeDetails(form.employee_id)>
        <cfdump var="#employeeDetails#">
    </cfif>
    <cfform action="#CGI.SCRIPT_NAME#">
    <cfselect name="employee_id">
    <option value="0">Employee</option>
    <option value="1">Carolynn Petersen</option>
    <option value="2">Dave Heartside</option>
    <option value="3">Linda Stewart</option>
    </cfselect>
    <cfinput name="sbmt" type="submit" value="Send">
    </cfform>

  • Passing javascript value to jsp param

    Hi all,
    I want to be able to pass the return value of function getUrl() to jsp param...Is this a correct way? it's not working.... What should I assign to the value of param?
    <HTML>
    <HEAD>
    </HEAD>
    <script language="javascript">
    function getUrl()
         var url=window.location;
         return url;
    </script>
    <BODY>
    <jsp:include page='GetFileServlet' flush="true" >
    <jsp:param name="url" value= "return getUrl()"/>
    </jsp:include>
    </BODY>
    </HTML>
    thank you for your time and assistance

    Hi all,
    I want to be able to pass the return value of
    function getUrl() to jsp param...Is this a correct
    way? it's not working.... What should I assign to
    the value of param?
    <HTML>
    <HEAD>
    </HEAD>
    <script language="javascript">
    function getUrl()
         var url=window.location;
         return url;
    </script>
    <BODY>
    <jsp:include page='GetFileServlet' flush="true" >
    <jsp:param name="url" value= "return getUrl()"/>
    </jsp:include>
    </BODY>
    </HTML>
    thank you for your time and assistanceThis can't be done, since the jsp call will be exercised on the server, before the page gets sent to the user, while the javascript gets performed on the client (well after the jsp is done its job).
    An all server-side solution would be what you needed:
    <HTML>
        <HEAD>
        </HEAD>
        <BODY>
            <jsp:include page='GetFileServlet' flush="true" >
                <jsp:param name="url" value= "<%=request.getRequestURL()%>"/>
            </jsp:include>
        </BODY>
    </HTML>

  • Populating values in jsp without submitting the form

    Hi,
    I have a jsp page in which i have 2 select boxes. The values to these select boxes should come from database. When i change the selection of select box, the corresponding values from database should be displayed in the other select box without submitting the form.
    How to accomplish that.
    Any one having any idea about how to implement this stuff, please reply.
    Thanks in advance,
    Joby.

    You should devide the JSP into two frames and make one frame size to 0 so it should not be visible to the use ,and put one jsp in this frame ,tht will work as the hidden jsp.
    now in other frame put ur main jsp and on the change event of th select call a java script function and in this function pass the parametr to the hidden jsp like
    parent.frames["hiddenframename"].document.location="hiddenJspName.jsp?parameter1=value1
    and now in hodden jsp populate the data frm database based on passed parameter and after getting the data fillup the select on the second frame.
    try it and if not work thn write me...
    Bye.

  • How to call a URL in jsp without direct the page to the URL

    Hi,
    I have a jsp page, which when user come in to the page, the page will call a URL without direct the current jap page the URL. This URL will only help me to do on something in the backend. Anyone know any method that i can use to call the URL?
    Thanks a lot

    Doeas AJAX rings a bell? ;-)
    https://blueprints.dev.java.net/bpcatalog/ee5/ajax/usingAJAXwithoutJSF.html

  • Passing Javascript values to JSP

    Hi,
    I have a select box and i get the selected value using the javascript. The variable that stores the value should be passed to the JSP. I need to know , how i an do the same.
    When a user clicks on a option I call a javascript function:
    function myFun()
    var text = document.myform.selectboxname.options[index].text;
    return text;
    I need to get the value returned.
    The select option is as follows.
    <select name="sel" multiple onChange=myFun()>
    How can i get the value "text".
    Kindly help.

    if yes.....my functions returns a value...how can i
    access the same.You can't, in the way you want it. You need the function to put the value where it's supposed to go.
    I'm sorry, but this question is clearly out of scope for this board. Look for a Javascript support forum. Just the fact that the HTML originated from a JSP doesn't make it a Java question.

  • How to pass JavaScript value to JSP variable

    I know this is not possible. But I need to do this :
    <script language = "Javascript">
    function findElement() {
    for(i=0; i<document.forms[0].elements.length; i++){
    if(document.forms[0].elements.type == 'text') {
    var elName = document.forms[0].elements.name;
    var elValue = document.forms[0].elements.value;
    // Display element name and value
    //alert(elName +' = '+ elValue)
    <%
    String elName = elName;
    System.setProperty(%>elName<%, %>elValue<%);
    %>
    </script>
    in JSP. I need to get the name and value of the textbox in the form to just change one property on the JSP System. Any help pls

    Thanks man, I think that would work.
    I found something else already though and it works on the same page
    <%
         Enumeration parameters = request.getParameterNames();
         String parameterValue = " ";
         String parameterName = " ";
                   while (parameters.hasMoreElements()) {
                        parameterName = (String) parameters.nextElement();
                        parameterValue = request.getParameter(parameterName);
                   out.println("NAME =========" + parameterName);
                   out.println("VALUE =========" + parameterValue);
                   //System.setProperty(parameterName, parameterValue);
         %>
    Thanks again for the help

  • Display error message without refreshing the entire jsp

    Hi,
    I want to display the server side error messages in the jsp without refreshing the entire jsp. How can this be achieved?
    I think there is some way with use of AJAX. If yes can you please elaborate on the same as to how to do this.
    Regards,
    Shwetha

    In the project it's presented:
    1) how the servlet can send JSON data depending on the request's parameter
    2) how to obtain this data on the client side and show in on the page using jQuery without refreshing the whole site
    What You need to do is just to send the error (as JSON data) insetad of the values that are passed now and display this error on the page.

  • Firefox was a good, but now it's slow and when I search google I have to refresh the page when I click the back button. This is driving me to other browsers.

    When searching in goole, If I click on a link and want to go back a page firefox does not load the previous google search without refreshing the page. Because I use google frequently this is very annoying. Firefox has also been freezing and crashing most frequently of any of the other browsers that I have been driven to use. I'm not sure why I should keep it on my computer at this point.

    Regarding the Google results page blanking out when you go back, this occurs when using a combination of three settings, which may be fairly common:
    * Zoom level higher than two steps (120%; the newest versions of Windows apparently default to 125%, which Firefox detects and adopts)
    * Google search set to instant prediction results (default on Google search, but can be disabled in Search settings, see [https://support.google.com/websearch/answer/186610 Google Instant Search Help])
    * Opening results in the same tab, then using the Back button to return to the results page (default on Google search, but can be changed in Search settings to open results in a new window/tab, see [https://support.google.com/websearch/answer/425 Google Search Help])
    Firefox's cache feature attempts to maximize speed for forward and back actions by storing the last rendering of the page. Something about the instant prediction results ''with zoom'' is causing Firefox to briefly display the results and then they blank out and you have to reload the page to get them back. The mystery is why this only occurs (so far as I have heard) on Google? Is one of Google's scripts blanking out the page? Does Google care that it's messing up Firefox users?
    For now, if you do not want to or cannot change any one of the three factors needed to experience this problem, you can disable the storage of the last rendering of the page. Then Firefox will skip the shortcut and lay it out fresh, which is known to work. As noted in earlier posts, you make this change using the about:config preferences editor.
    (1) In a new tab, type or paste '''about:config''' in the address bar and press Enter. Click the button promising to be careful.
    (2) In the search box above the list, type or paste '''sess''' and pause while the list is filtered.
    (3) Double-click the '''browser.sessionhistory.max_total_viewers''' preference and change it to 0 (that's a zero).
    This thread has a lengthy discussion: [https://support.mozilla.org/en-US/questions/970863 When I go back to Google search results page, it takes me to a blank Google screen].

  • Button click without "refresh" the current page. Please help

    Hi everyone:
    Here is my code:
    Code:
    <HTML>
    <HEAD>
    <TITLE>Parallel Port</TITLE>
    </HEAD>
    <BODY>
    <H1 ALIGN="CENTER">On/Off Bit0 of server's parallel port</H1>
    <form name="myform" action="robotcontrol.jsp" method="POST">
    <input type="hidden" name="action" value="<%=action%>">
    <INPUT TYPE="SUBMIT" VALUE="set/reset">
    <jsp:include page="./VideoToApplet/htmlroot/index.html" flush="true" />
    </form>
    </BODY>
    </HTML>
    everytime i click the button, it will perform (set/reset) parallel port, and i have include a html page which consist a video streaming applet.Maybe cause of this:
    <form name="myform" action="robotcontrol.jsp" method="POST">
    So.. please help !! Thanks in advance.
    but unfortunately, everytime i click on the button, it will likely refresh the page, and the applet will auto reconnect to camera,(it take time to reconnect)..

    Inside the body of JSP/HTML page, at the end of <BODY>, paste this small script.It will refresh your page automatically after 20 seconds, that is nothing but the value assigned to V.You can assign the time interval as you wish.
    <SCRIPT language="javascript">
         var ONESEC = 1000 ;                    // One second (in ms)
         var V = 20 * ONESEC ;
         var ONEMIN = 60 * ONESEC ;     // One minute (in ms)
         var INTERVAL = 1 * ONEMIN ;               // How often is page refreshed (in ms)
         window.setTimeout(function(){
                                            window.location.reload() ;
                                            },V) ;
    </SCRIPT>

Maybe you are looking for