This article summarizes several examples of getting the value of the check box in jsp, also introduces the methods to solve the Chinese and English values, and finally describes in detail how to get the value and save it to the database.
Obtain the values value of the check box from the JSP page, store the value in the array by defining the array, and display it
The simplest is
| The Code is as follows: |
Copy code |
|
String strLove = "";
String [] strLoves = (String []) request. getParameterValues ("love ");
// Read each selected item cyclically
For (String love: strLoves)
{
StrLove = strLove + love + ",";
}
StrLove = strLove. substring (0, strLove. length ()-1 );
|
Example
Html page
| The Code is as follows: |
Copy code |
|
<Form action = "02.jsp" method =" post ">
Name: <input type = "text" name = "uname"/>
<P>
Skills:
<Input type = "checkbox" name = "tech" value = "J2EE"/> J2EE
<Input type = "checkbox" name = "tech" value = ". NET"/>. NET
<Input type = "checkbox" name = "tech" value = "ASP"/> ASP
<Input type = "checkbox" name = "tech" value = "PHP"/> PHP
<P>
<Input type = "submit" value = "submit"/>
</Form> |
Jsp processing page
| The Code is as follows: |
Copy code |
|
<% @ Page contentType = "text/html; charset = GB2312" %>
<Html>
<Head>
<Title> 02.jsp</title>
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
</Head>
<Body>
<%
Request. setCharacterEncoding ("GB2312 ");
String Name = request. getParameter ("uname ");
// Obtain the parameter Array
String Tech [] = request. getParameterValues ("tech ");
%>
<H1> Name: <% = Name %>
<H1> good at technology:
<%
// Output Array
Int I;
For (I = 0; I <Tech. length; I ++)
{
%>
<% = Tech [I] %>
<%
}
%>
</H1>
</Body>
</Html> |
The above code is not in English or Chinese. If the option value contains Chinese, the processing code in JSP is as follows:
| The Code is as follows: |
Copy code |
|
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
<%!
Public String chinese (String sText)
{
Try
{
Return new String (sText. getBytes ("iso-8859-1"), "gbk ");
}
Catch (Exception e)
{
Return sText;
}
}
%>
<%
String strLove = "";
String [] strLoves = (String []) request. getParameterValues ("love ");
// Read each selected item cyclically
For (String love: strLoves)
{
StrLove = strLove + chinese (love) + ",";
}
StrLove = strLove. substring (0, strLove. length ()-1 );
%> |
Jsp obtains the checkbox value and writes it to the database. You can use the following methods:
| The Code is as follows: |
Copy code |
|
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> test </title>
</Head>
<Body>
<Form name = "myform" id = "myform" method = "post" action = "CorAdd. jsp">
<Label>
<Input type = "checkbox" name = "CorpMode" value = "1"/> item
</Label>
<Label>
<Input type = "checkbox" name = "CorpMode2" value = "2"/> head
</Label>
<Label>
<Input type = "checkbox" name = "CorpMode2" value = "3"/> others
</Label>
<Input name = "" type = "button" onclick = "document. myform. action = 'submit. jsp? Menu = add' "/>
</Form>
</Body>
</Html> |
Save the jsp program on the page
| The Code is as follows: |
Copy code |
|
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" import = "java. util. *, java. text. *, java. SQL. * "%>
<%
If (menu. equals ("add "))
{
String tmp = "";
String SCorpMode = "";
String [] CorpMode = request. getParameterValues ("CorpMode ");
If (CorpMode. length> 0)
{
For (int I = 0; I <CorpMode. length; I ++)
{
SCorpMode = SCorpMode + CorpMode + ",";
}
SCorpMode = SCorpMode. substring (0, SCorpMode. length ()-1); // remove the last comma in the SID.
}
Tmp = "insert into Corporation (CorpMode) values ('+ SCorpMode + ');
If(dbc.exe cuteUpdate (tmp)> = 0)
Out. println ("<script> alert ('information added successfully! '); Location. href = 'corpadd. jsp' </script> ");
Else
// Out. println ("<script> alert ('failed to add! '); Location. href = 'corpadd. jsp' </script> ");
Out. print (tmp );
}
}
%> |