1. Background:
We all know that the <select> control is a drop-down selection box. In JSP, when one of the items is selected for submission, the selected values will be submitted to the Servlet class for processing. When a page similar to the submission page is returned, the page has the same <select>. The items in the selection box are read from a table in the database, in this case, you do not need to use HttpSession for processing. The order of values in the select control is the order in the database table, rather than the value we selected. let's solve this problem.
2. Session solution:
Form 1:
<% Dbmanager DB = new dbmanager (); string SQL = "select * from S"; resultset rs=db.exe cutequery (SQL ); %> <form action = "servlet" method = "Post"> <Div class = "E"> <select name = "E"> <% while (RS. next () {string securityname = Rs. getstring ("name"); out. println ("<option value = '" + name + "'>" + name + "</option> ");} %> </SELECT> <select name = "timeselect"> <option value = "1 hour"> 1 hour </option> <option value = '1 year'> 1 we Ek </option> <option value = '1 year'> 1 month </option> <option value = '1 year'> 1 year </option> </SELECT> <input type = 'submit 'class = "btniconleft mr10" value = 'query'/> </div> </form> <% IF (RS! = NULL) Rs. Close (); DB. closecon (); %>
Form 1 submits the selected values of the two <select> controls to the Servlet class for further processing. In the servlet class, the two selected items in Form 1 are placed in the Session, respectively, after processing, jump to Form 2.
Form 2:
<Form action = "servlet" method = "Post"> <Div class = "E"> <select name = "E"> <% httpsession session = request. getsession (); string name = (string) session. getattribute ("name"); // retrieves the first <SELECT> selected string time = (string) session in Form 1. getattribute ("time"); out. println ("<option value = '" + name + "'>" + name + "</option>"); // output the while (RS) selected in Form 1 first. next () {string securityname = Rs. getstring ("name"); If (securityname. equal S (name) // read the database. If it is in the session, skip continue; out. println ("<option value = '" + name + "'>" + name + "</option> ");} %> </SELECT> <select name = "timeselect"> <% Out. println ("<option value = '" + time + "'>" + time + "</option>"); If (! "1 hour". Equals (time) Out. println ("<option value = '1 hour '> 1 hour </option>"); If (! "1 week". Equals (time) Out. println ("<option value = '1 week '> 1 week </option>"); If (! "1 month". Equals (time) Out. println ("<option value = '1 month'> 1 month </option>"); If (! "1 year ". equals (time) out. println ("<option value = '1 year'> 1 year </option> "); %> </SELECT> <input type = 'submit 'class = "btniconleft mr10" value = 'query'/> </div> </form>
Note:
Form 1 contains two <select> and a submit button, and the option of the first select element is selected from a database table. After selecting the two items, click submit and process them in the Servlet class. The two items selected in Form 1 are placed in the Session. After processing, they are redirected to Form 2. In this case, the Session method is adopted to make Form 2 first display the two items we selected in Form 1. That is, extract the two options from the Session and output them first, and then read the database in the while loop. If there is a Session, continue to skip.