Passing value from javascript function to servlet

Hello everybody,

i need to pass parameter from javascript function to servlet.

what i wrote is :

function callPopulateServlet(t)

{

var h =document.NewRequest.services;

var y = t.selectedIndex;

alert(h.options[y].value);

var id=h.options[y].value;

<%session.setAttribute("id",id);%> // am getting error at this point

document.NewRequest.submit();

}

with this id am quering values from database through servlet.

any body knows plz help me.

thanks,

anil.

[551 byte] By [anilmekala.reddya] at [2007-11-27 11:48:31]
# 1

What is the error you are getting? Do you have a stack trace?

maple_shafta at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 2

this is the error am getting

Stacktrace:

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)

org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)

org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:299)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)

javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

anilmekala.reddya at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 3

Plz responsed for this error...

anilmekala.reddya at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 4

> Plz responsed for this error...

Your JSP won't compile. This has nothing to do with passing any values from forms anywhere.

cotton.ma at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 5

can i set session.setAttribute("id",id) for javascript variable..

anilmekala.reddya at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> can i set session.setAttribute("id",id) for

> javascript variable..

Please stop calling them JavaScript variables. They are not JavaScript variables. All your JSp (or Servlet) is aware of is form values.

Moving on... do you understand yet that your problem is a compilation error? You need to fix this first before moving on to addressing whatever other problems you might have.

cotton.ma at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 7

Do yo know the difference between client and server?

Do you know what JSPs do?

Do you know what Javascript does?

Why the fsck are you working on a JSP, if you don't even grasp the merest basics about web apps?

Of course you get an error. id isn't even declared.

CeciNEstPasUnProgrammeura at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 8

Takiing a look at your unformatted code...

function callPopulateServlet(t)

{

var h =document.NewRequest.services;

var y = t.selectedIndex;

alert(h.options[y].value);

var id=h.options[y].value;

<%session.setAttribute("id",id);%> // am getting error at this point

document.NewRequest.submit();

}

I see that you are lost.

No you cannot do this. That is banana-cakes. JavaScript runs on the client. Your JSP runs on your server. So you can't midway through a JavaScript function (executing on the client browser) suddenly do something on the server side. That server side code is executing long before your JavaScript code is ever run.

I expect your error is being caused by you attempting to modify headers after sending content.

cotton.ma at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 9

> Please stop calling them JavaScript variables. They

> are not JavaScript variables. All your JSp (or

> Servlet) is aware of is form values.

They *are* Javascript variables. He's mixing Javscript code with JSP scriptlets. I suppose it's so frigging asinine that your brain refuses to realize it.

CeciNEstPasUnProgrammeura at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 10

> > Please stop calling them JavaScript variables.

> They

> > are not JavaScript variables. All your JSp (or

> > Servlet) is aware of is form values.

>

> They *are* Javascript variables. He's mixing

> Javscript code with JSP scriptlets. I suppose it's so

> frigging asinine that your brain refuses to realize

> it.

At first glance I thought he was passing id from a JavaScript variable to a hidden form variable named id and then submitting.

You're right though... I assumed too much intelligence here.

cotton.ma at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...
# 11

plz see the following discussion for passing argument from Servlet to Javascript

http://forum.java.sun.com/thread.jspa?threadID=677224&messageID=3951917#3951917

shagila at 2007-7-29 18:19:44 > top of Java-index,Java Essentials,Java Programming...