Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing parameters from jsp to servlet

 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you getting?
We might be able to help you if you post your code so we can see what's happening.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Servlets forum.
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What values are you getting ??
 
Anoop Kavalloor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what method in your servlet are you calling reqeust.getParameter?
 
Wayan Suwastika
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic