Vijay Kumar Khushlani
Vijay Kumar Khushlani

Reputation: 35

Store a java script value in jsp String variable

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 welcome.

Upvotes: 2

Views: 17453

Answers (5)

user2575725
user2575725

Reputation:

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.

Upvotes: 0

Chic
Chic

Reputation: 10569

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 completely 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 recommend 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

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 122026

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

So use some hidden variables like here

Upvotes: 0

PSR
PSR

Reputation: 40348

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.

Upvotes: 0

A4L
A4L

Reputation: 17595

Java script is run on the client, JSP is run on the server. If you want your JS-variable to come 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")

Upvotes: 0

Related Questions