1

Here I want to get 2 option value, the project.id, and project.name

<select name="project" id="project">
            <option value="0" Selected>Select Project</option>
            <c:forEach var="project" items="${project}">
            <option value="${project.id project.name}">${project.name}</option>
            </c:forEach>
        </select>

and then in my servlet, I want it to be like this, is this possible?

int projectId = Integer.parseInt(request.getParameter("project"));
int projectName = request.getParameter("project");

how do I do this?

Chong
  • 59
  • 1
  • 7
  • It is possible. You could do something like – rickz Feb 03 '16 at 02:37
  • its not working, im having error: – Chong Feb 03 '16 at 02:46
  • java.lang.StringIndexOutOfBoundsException: String index out of range: -1 – Chong Feb 03 '16 at 02:46
  • Thank you my friend, its working already! – Chong Feb 03 '16 at 02:50
  • You could post your working code as a solution below here. – rickz Feb 03 '16 at 03:04
  • I did it wrong. It is easier to use String projectName = project.substring(project.indexOf("|") + 1); – rickz Feb 03 '16 at 03:18
  • Were you able to resolve this? – Perdomoff Feb 03 '16 at 19:09

1 Answers1

0

you have to write the value in option by using the seperator. Now at servlet end, use split function to seperate the values and use them. check this answer : split string function.

Or, you can use javascript to do so, for that you have to take two other hidden inputs to pass values. check the script :

function splitvalue(){
 var optval=document.getElementById("instno").value;
 var valarray=optval.split(',');
 
 for(var i=0;i<valarray.length;i++){
  document.getElementById("number").value=valarray[0];
  document.getElementById("text").value=valarray[1];
 }
}
<select id="instno" name="instno" onchange="splitvalue();">
<option value="1,abc">abc</option>
  <option value="2,xyz">xyz</option>
</select>
<input type="text" id="number" name="number"/>
<input type="text" id="text" name="text"/>

Here in place of input text, you can use input type="hidden" to hide inputs. and pass these to servlet instead of select option values.

Community
  • 1
  • 1
jatz2012
  • 31
  • 9