The front-end selects multiple options on the JSP page and uploads these values to the servlet to complete the addition of the class course. In JSP, multiple selections are implemented using checkbox. All chekboxes share the same name, and a string array is received in the background. It can be used to receive: String [] string name = request. getparametervalues ("form name "); Then the length of the array can be obtained using the. lenth attribute of the string. Except for the first list, only the selected values can be uploaded to the backend, and all others will be uploaded together. Whether selected or not, you can use the following mark to determine which values are selected, the subscript can be uploaded to the background through a hidden domain, The HTML tag of the hidden field is: <input type = "hidden" value = "" name = ""> -------------------------------------------- JSP page -------------------------------------------------------- <Form action = "<% = basepath %> Servlet/termcourseserver? Pattern = added" Method = "Post"> <Table border = "1" align = "Left"> <Tr> & Lt; th width = "110" & gt; Class: </Th> <TD> <Select name = "termid"> <% For (INT I = 0; I <terms. Size (); I ++ ){ Term term = terms. Get (I ); %> <Option value = "<% = term. GETID () %>"> <% = Term. gettermname () %> </Option> <% } %> </SELECT> </TD> </Tr> <Tr> <TH> Select </Th> <TH> Name </Th> <TH> Instructor </Th> <% For (INT I = 0; I <courses. Size (); I ++ ){ Course course = courses. Get (I ); %>
<Tr> <TD> <Input type = "hidden" value = "<% = course. GETID () %> "name =" courseid "> // use a hidden domain to receive the value of courseid. The following check box accepts the value of the lower mark. <Input type = "checkbox" value = "<% = I %>" name = "Index"> </TD> <TD> <% = Course. getcoursename () %> </TD> <TD> <Select name = "teaid"> <Option value = "null"> --- Select a teacher --- </Option> <% Arraylist <teacherinfo> teachers1 = new jtermcourses (course) . Getteachers (); // encapsulated jtermcoursebean; For (Int J = 0; j <teachers1.size (); j ++ ){ Teacherinfo Teacher = teachers1.get (j ); %> <Option value = "<% = teacher. GETID () %>"> <% = Teacher. getteaname () %> </Option> <% } %> </SELECT> </TD> </Tr> <% } %> </TD> </Tr> <TD align = "center" colspan = "2"> <Input type = "Submit" value = "OK"> & Nbsp; <Input type = "reset" value = "cancel"> </TD> </Tr> </Table> </Form> ------------------------------------------- Servlet page ------------------------------------------- Private void added (httpservletrequest request, httpservletresponse response) Throws servletexception, ioexception { String termid = request. getparameter ("termid "); String [] courseids = request. getparametervalues ("courseid"); // receives the value of the foreground, which is a one-to-one array. String [] teaids = request. getparametervalues ("teaid "); String [] indexs = request. getparametervalues ("Index "); For (INT I = 0; I <indexs. length; I ++ ){ Int nindex = new INTEGER (indexs [I]); // new INTEGER (): converts data into integer variables. Used to obtain the tag Value Termcourse = new termcourse (); Termcourse. settermid (termid ); Termcourse. setcourseid (courseids [nindex]); // you can use the array subscript to obtain the selected value, which is the same as the following. Termcourse. setteaid (teaids [nindex]); New termcoursedao (). Add (termcourse ); } Response. sendredirect ("termcourseserver? Pattern = Add "); // return to the added servlet to the added page. } |