This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes passing parameters from jsp to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing parameters from jsp to servlet" Watch "passing parameters from jsp to servlet" New topic
Author

passing parameters from jsp to servlet

Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

Hi,

I have a select box in my jsp page. onChange() of the select box, a javascript function is called that submits the form and calls a servlet where I am trying to retrieve the selected value(of the select box in jsp) using request.getParameter("") but I am not able to retrieve the value.

Please help me out in this..


Thanks,
Nidhi
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What are you getting?
We might be able to help you if you post your code so we can see what's happening.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Moved to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
What values are you getting ??


SCWCD 1.4, SCJP 1.4
Anoop Kavalloor
Greenhorn

Joined: Sep 06, 2004
Posts: 15
I am trying to retrieve the selected value(of the select box in jsp) using request.getParameter("")


u have to use the name of the select box. say if the name of the select box in the jsp page is "name", then ur request will be like this...request.getParameter("name") -- in the servlet.


I am not completely useless.<br />I can be used as a bad example.<br /> <br />SCJP 1.4
Nidhi Singhal
Ranch Hand

Joined: Sep 19, 2004
Posts: 89

Hi

My Code is as following:

In the jsp:

<FORM name="qMain" action="../qServlet" method="get" style="FLOAT: left" >

<SELECT class = selectStyle name="stream_name" id="stream_name" onChange="submitfrm(this.form);" size="1">

function submitfrm(fm)
{
alert(fm.stream_name.options[fm.stream_name.selectedIndex].text);
fm.submit();
}

//I am getting the selected value in the alert box


In the Servlet:

String p_stream = req.getParameter("stream_name");

System.out.println("Value for Stream : " + p_stream );

//here I am not getting any value

the S.O.P. displays:

Value for Stream :


Regards,
Nidhi
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Originally posted by Nidhi Singhal:
Hi

My Code is as following:

In the jsp:

<FORM name="qMain" action="../qServlet" method="get" style="FLOAT: left" >

<SELECT class = selectStyle name="stream_name" id="stream_name" onChange="submitfrm(this.form);" size="1">

function submitfrm(fm)
{
alert(fm.stream_name.options[fm.stream_name.selectedIndex].text);
fm.submit();
}

//I am getting the selected value in the alert box



Nidhi,

I think since you are doing a form.submit() in the JSP, u have to manually add all the parameters that you want to pass alongwith the request.

Something like this:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

u have to manually add all the parameters that you want to pass alongwith the request.


No, that is absurd.

Whether the form is submitted by a button click or under Javascript control, the form elements will be submitted with the form.

The posted HTML looks pretty sloppy. I'd suspect HTML formatting issues. Why not perform a View Source of the actual page and show us the entire form section.
[ September 08, 2005: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

fm.action = fm.action+ "?stream_name="+fm.stream_name.options[fm.stream_name.selectedIndex].text;

Danger: query values are NOT supposed to be included on the form action. This is highly vendor specific behaviour and is best avoided. Try Bear's suggestion first.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

From what method in your servlet are you calling reqeust.getParameter?
Wayan Suwastika
Greenhorn

Joined: May 25, 2005
Posts: 11
I think there is some thing miss on your form element. To get the value from the your select box may be the method of your form should looks like :

<FORM name="qMain" action="../qServlet" method="POST" style="FLOAT: left" >
<SELECT class = selectStyle name="stream_name" id="stream_name" onChange="submitfrm(this.form);" size="1">

I change the GET to POST method.
And also make sure your servlet code are correct on overriding doGet() or doPost() method. To support both type GET & POST it better you override the two method doGet() and doPost()

rgds
wyasa


SCJA, SCJP 1.4, SCWCD inprogress
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing parameters from jsp to servlet