Embeding javascript code within jsp code


i have a javascript variable

var monthNum and i assigned some value in javascript.Now i use that variable in jsp as

pstatement2 = connection.prepareStatement(“Select * from importtimetable where month='”+monthNum+“'”);
In that where condition monthNum is jascript variable. I used like that i got a error.

Could plz tell that correct syntax i have to use in that place.

Doesn’t really work that way.

You can use a Java/JSP variable in Javascript, but since Javascript is client side it can’t go the other way.

Remember the JSP gets translated into an actual Java class. So anything in the JSP that’s not Java gets turned into something like … out.println(“var monthNum”) … etc… which is just a String (as far as Java is concerned).

The best thing you can do is set the Javascript variable, submit it to the server, and then use it on the next request.

There are many ways to do this, but I’m not sure exactly what you’re trying to do.

Cannot use javascript variable in JSP directly. You need to set in JSP implicit object e.g request object
in hidden field of form or text box of form and submit this page


String javaScriptVariableNowInJSP = request.getParameter(“hiddenFieldName”);

can use

or AJax is another option to set javascript variable in server side