3

below is my code (1.jsp)

<html>
<head>
  <script type="text/javascript">

   function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
  document.write("\n value is"+selectedValue);
  }

 </script>
</head>
 <body>
<form method="post" action="SampServlet">
  <select id="selectBox" name="selurl" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
</form>
 </body>
</html>

Here I have inserted this code into a jsp page.And getting the value of "selectedValue" from javascript to scriptlet with in the same jsp like this.

<% String val=(String)request.getParameter("selurl");
System.out.println("\n selected value is:"+val); %>

I am getting selected value as null as output. And if I print javascript selectedValue parameter it is giving me correct output i.e.,output as the option selected.But in scriptlet am getting null.Where is the error.I included all headers and directives.Please help me.

user2365917
  • 1,095
  • 5
  • 15
  • 21
  • I have tried with the approaches present in http://stackoverflow.com/questions/5701031/how-do-i-pass-javascript-values-to-scriptlet-in-jsp but not gettin..Still getting null value – user2365917 May 20 '13 at 06:35

4 Answers4

2

In your web browser you have only html, javascript and css. All JSP code is meant to be run on the server. So you get only the output of the jsp file. And after this you cannot change the jsp code.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • Then how can I get the value of selected option in jsp scriptlet.Please help me.I need this value to send to servlet and get the details of corresponding option(selected option).Please how can I get the value from javascript. – user2365917 May 20 '13 at 06:38
  • You don't need to change the scriptlet. On the `onchange` event you just need to make a Ajax request to the servlet with your new values. – me_digvijay May 20 '13 at 06:41
  • How can I make ajax request.Because am new to ajax.and while running is it needed to add any jar files to run ajax? – user2365917 May 20 '13 at 06:44
  • @user2365917: Ajax can simply be done JavaScript. Nothing Needed. Everything is done on client-side. For now you can use http://api.jquery.com/jQuery.ajax/ method to make ajax request. And here is a very good tutorial on AJAX https://developer.mozilla.org/en-US/docs/AJAX – me_digvijay May 20 '13 at 06:56
2

Use submit Button to get Your Selected value at same page and no need any function,no need onsubmit.

for example:

<form method="post" action="">
 <select id="selectBox" name="selurl">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
<input type="submit" value="Submit" name="sub">
                                                //use scriplet tag 
<% String r=request.getParameter("sub");
if(r.equals("Submit"){
String s=request.getParameter("selurl");
System.out.println("selected value is "+s);
}%>
</form>
151291
  • 3,308
  • 7
  • 48
  • 81
0

Your select element should have a name attribute and you must use that name in request.getParameter()

<select id="selectBox" name="selurl"">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
</select>


String val = request.getParameter("mySelect");

EDIT:

If you want the server request to be made on the select element's onchange event, you must Ajax.

Using jQuery,

$.post('SampServlet', {selectedValue: selectedValue}, function(data) {
//Update view
});
c.P.u1
  • 16,664
  • 6
  • 46
  • 41
0

You're missing the <submit> button.

<form method="post" action="SampServlet">
  <select id="selectBox" name="selurl" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
  <input type="submit" /> <!-- MISSING!! -->
</form>

Add the button and click on it to submit the form to your servlet with the selected value sent as selurl. Please, note that your form's action attribute is pointing to SampServlet which seems to be a servlet. For jsps we usually have something like action="page.jsp" in the form.

EDIT:
If you want to post the form automatically when the user selects a value from the drop-down just set your onchange to: (you won't even need the <submit> button then)

onchange="this.form.submit()"
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • I am not going to submit.Just I want to select the option.And when I choose one option then the data related to that option should be printed on browser from getting data from database.I am following MVC.So In my jsp I need to send this selected option to servlet.Which will get data from javabean(database) and return to jsp.And then jsp displays it.This is my task.But I am unable to get selected value in jsp. – user2365917 May 20 '13 at 06:48
  • JSP is server-side mate. JSP scriptlets `<% %>` won't ever execute on the client-side i.e. without doing a submit. If the app requires no submit then you must use Ajax/jQuery. But remember this would invoke a servlet that would talk to a DB and return the response that you can then ONLY use via Javascript. Which is fine in my opinion but there's no way to have your JSP code block process this response now since it has been fetched at client-side not server-side. – Ravi K Thapliyal May 20 '13 at 06:55
  • If I use submit() after selection is my problem got resolved? – user2365917 May 20 '13 at 06:58
  • Yes, you'll see the selected value in your Tomcat's console log (since you used System.out). To print on the webpage just use `

    The selected value is <%= request.getParameter("selurl") %>

    ` (Notice there's no semicolon in <%= %> block.) Also your form's `action` should point to this jsp file so that it receives the post data.
    – Ravi K Thapliyal May 20 '13 at 07:01
  • Then which event should I use in my – user2365917 May 20 '13 at 07:04
  • What is your use case? What should happen when a user selects a particular option? This would help us suggest if Ajax or submitting the form makes sense here. Better update this in your question. Would help all the new people as well that see your question. – Ravi K Thapliyal May 20 '13 at 07:06
  • In my application drop down list should take the list of available categories from database.So my ListServlet gets the details from java bean which takes from database and puts them into a list.And forwards the list and request to view.jsp.This jsp displays those options as a drop downlist. From drop downlist user selects one option say "fruits".Then my jsp should take the selected option "fruits" and forwards the "fruits" i.e., selected value to another servlet say "fruitsServlet".fruitsServlet processes and sends fruits list to jsp for displaying.Please help me.getParameter is giving null. – user2365917 May 20 '13 at 09:04
  • In your dropdown jsp where you have the form set its `action` to `fruitsServlet` and add a `submit` button. This will make the selected value available to `fruitsServlet` via request.getParameter("selurl"); Now in fruitsServlet.doPost() fetch the list of fruits that you want to display from DB based on `selurl`. Once fetched, set this List using request.setAttribute("fruitsList",listOfFruits); in the servlet before forwarding the request to your final jsp. Your final jsp would then just do a foreach(String fruit: request.getAttribute("fruitsList")) to display all the fruits to the user. – Ravi K Thapliyal May 20 '13 at 09:17
  • Thank you.But my doubt is this entire thing is present in only one jsp.i.e., view.jsp Here the problem is for getting the details from ListServlet for the first time to make drop down list I am using form action="ListServlet" and getting list.Now after user selects one option how can I set form action="fruitsServlet" with in the same jsp page.I want to use only one jsp(view.jsp) – user2365917 May 20 '13 at 09:18
  • Then do a post to the same `ListServlet` (although real word applications don't work with a single .jsp; if you're being taught that this is MVC then it's incorrect!) and in `doPost()` do an if-else on `selurl`. If it's `null` you fetch options else you process your list of fruits and redirect to the same `view.jsp`. Do a similar if-else in view.jsp as well. If `listOfFruits` is null, display options otherwise show the list of processed fruits. Simple! – Ravi K Thapliyal May 20 '13 at 09:24
  • Thank you.Your suggestions were so helpful.I will try to use one more jsp inorder to follow mvc. – user2365917 May 20 '13 at 09:31