admin管理员组

文章数量:1334826

Can anyone help me to solve my simple problem of my jsp code? here is my jsp page with JavaScript

function fid()
{
    var id=document.getElementById("id").value;
    alert(id);
    <%
             String demo=id;             // store id value of java script but not working
             or find(id);                // or call a function with parameter of java script var value
                                       // both are not working
     %>
}

here is my html page --

Enter Student Id<input type='text' id='id'><button type='button' onclick='fid()'>Find</button>

How do I pass the js value in jsp function.
I know that I can do this with ajax or jquery but I don't know these languages. Any suggestions or code snippets are wele.

Can anyone help me to solve my simple problem of my jsp code? here is my jsp page with JavaScript

function fid()
{
    var id=document.getElementById("id").value;
    alert(id);
    <%
             String demo=id;             // store id value of java script but not working
             or find(id);                // or call a function with parameter of java script var value
                                       // both are not working
     %>
}

here is my html page --

Enter Student Id<input type='text' id='id'><button type='button' onclick='fid()'>Find</button>

How do I pass the js value in jsp function.
I know that I can do this with ajax or jquery but I don't know these languages. Any suggestions or code snippets are wele.

Share Improve this question edited Feb 26, 2013 at 16:06 Kelly S. French 12.4k10 gold badges64 silver badges93 bronze badges asked Feb 26, 2013 at 15:41 Vijay Kumar KhushlaniVijay Kumar Khushlani 351 gold badge2 silver badges9 bronze badges 4
  • i used this code . but i did work properly . is i am need to change to work it - window.location.replace("hello.jsp?id="+id); and then i jsp code - System.out.println(request.getParameter("id")); in url value is passed . but printing is "null" . is this code will help me ? – Vijay Kumar Khushlani Commented Feb 26, 2013 at 15:45
  • i also want to tell u guys . i have to print it on some page . i cant do it on next servlet page . so don't tell me that get the value on next page . that why i wont to use js , ajax , jquery . i know that i can get the value on next page . i have to it on some servlet page . – Vijay Kumar Khushlani Commented Feb 26, 2013 at 15:54
  • 1 To begin with, you can't call a scriptlet function from JavaScript, you need to submit it to a Servlet or to another JSP page and process it there, this is how it works. Please explain what's your real problem. By the way it looks, you sound like doctor something hurts, please tell me what I have. – Luiggi Mendoza Commented Feb 26, 2013 at 22:26
  • 1 @VijayKumarKhushlani could you please tell what you are trying to achieve ? It seems you want to something on same page using scriptlet, if you give little what you trying to achieve then only some will able to provide solution – Panther Commented Dec 8, 2014 at 17:49
Add a ment  | 

5 Answers 5

Reset to default 1

Server-side vs Client-side

In order to allow your server to respond to user input there has to be a request sent. You can ask your server can send to send a response in one of two ways:

  1. A pletely new page - such as when a link is clicked and a new HTML page loaded
  2. A AJAX response - run asynchronously and formatted however you want so that your client side JavaScript can use it.

But the server cannot manipulate the DOM of your existing page by itself. Even with AJAX, it has to have some code (usually JavaScript) on the client side to manipulate the page.

Normally if you are doing a search, I would remend option 1. This has the benefit of being simpler and it allows your users to navigate back/forwards or bookmark their search (assuming you make a GET call vs POST). See When to Use AJAX and When Not To

Using AJAX

If you want to use AJAX, make a call to your server create your xmlhttp object and then then you can pass your variable as @A4L mentioned only using AJAX. It would look something like this:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // Do what you want with the results from your server
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
var xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET",'myJSP.jsp/?id=' + id,true);
xmlhttp.send();

Then your server can pull in the parameter like @A4L mentioned. You may want to have your response just be straight text or an HTML block that the javascript can place inside a DIV but that's all up to you.

Disclaimer: I'm no expert in AJAX. Most of this is something you can learn from the W3 Schools site

Java script is run on the client, JSP is run on the server. If you want your JS-variable to e to the server and thus be accessible from the JSP than you have to send it as request parameter to the JSP using something like var url = 'myJSP.jsp/?id=' + id and in your jsp you can retrieve it using request.getParameter("id")

Your javascript values are client-side, your scriptlet is running server-side. So if you want to use your javascript variables in a scriptlet, you will need to submit them.

To achieve this, either store them in input fields and submit a form, or perform an ajax request. I suggest you look into JQuery for this.

Java script is Client side and JSP runs on server side .

So use some hidden variables like here

This solution does not involves js. As you said your js and jsp scriplet are on the same jsp. Try this instead:

<%
//find form was submit, if cmd is not null
if(null != request.getParameter("cmd")) {
   String id = request.getParameter("id");
   //your logic to find
}
%>

<form action='same.jsp'>
   Enter Student Id: <input type='text' id='id' value='${param.id}'/><!-- ${param.id} to restore the previously entered value by user -->
   <button name='cmd'>Find</button><!-- removed onclick, added name -->
</form>

P.S. avoid using jsp scriplets, use JSTL instead.

本文标签: javascriptStore a java script value in jsp String variableStack Overflow