Reputation: 203
Let say I have two servlet called servlet1 and servlet2.
Servet1
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
...
String htmlFormat = "<h2>Choose a employee</h2>" + "<p>Employee Name: <select name=\"employeeName\" onchange=\"location = this.options[this.selectedIndex].value;\">";
for (Employee employee: employees) {
htmlFormat += "<option value=\"servlet2\">" + employee.getFirstName() + " " + employee.getLastName() + "</option>";
}
htmlFormat += "</select></p>";
out.print(htmlFormat);
}
As you can see, when i click on the one of the option, it lead me to a servlet2. I would like to know do i pass user's click option (employee first name + last name ) value to another servlet. I need to get the firstname and lastname on the servlet2. Please help me.. Thanks
Upvotes: 0
Views: 1362
Reputation: 28
In your first servlet, have it output an html form that will POST the formdata back to the server:
<form id="usersubmit" action="servlet2"></form>
Make your selectbox part of the formdata. If you want the form to be submitted onchange you can use javascript:
<select name="employeeName" form="usersubmit" onchange="this.form.submit()">
...
</select>
Lastly the value
attribute of each <option>
tag should contain the values you wish to send/retrieve, so in your servlet you can write:
htmlFormat += "<option value=\" " +employee.getFirstName()+ " " +employee.getLastName()+ " \">" + employee.getFirstName() + " " + employee.getLastName() + "</option>";
This should send the value of the selected option
to "servlet2". To access the value in servlet2 use:
request.getParameter("employeeName");
Upvotes: 1
Reputation: 114347
This action takes place on the client, not the server. You need to POST the HTML form back to the server and access the employeeName
field from the FORMDATA.
Upvotes: 1