index.jsp
<form action= "Index.gj?method=toradio" method= "POST" >
<div align= "center" >
Gender: <input type= "Radio" name= "Sex" value= "1" > Male
<input type= "Radio" name= "Sex" value= "0" > Female
<br>
Hobbies: <input type= "checkbox" Name= "W" value= "Play" > Play
<input type= "checkbox" Name= "S" value= "sleep" > Sleep
<input type= "checkbox" Name= "C" value= "eat" > Eat
<br>
<input type= "Submit" value= "Submission" >
</div>
</form>
confirm.jsp
<form action= "Index.gj?method=tosubmit" method= "POST" >
<div align= "center" >
<br/><br/>
Gender: <input type= "Radio" name= "Sex" value= "1"
${(list.sex) ==1? " Checked ":"}/> Male <input
Type= "Radio" name= "Sex" value= "0"
${(list.sex) ==0? " Checked ":"}/> Female
<br>
Hobbies: <input type= "checkbox" Name= "W" value= "Play" <c:if test= "${list.hb.contains (' Play ')}" >checked= "Checked" </c :if> > Play
<input type= "checkbox" Name= "S" value= "Sleep" <c:if test= "${list.hb.contains (' Sleep ')}" >checked= "Checked" </c:if > > Sleeping
<input type= "checkbox" Name= "C" value= "Eat" <c:if test= "${list.hb.contains (' Eat ')}" >checked= "Checked" </c:if > > Eating
<input type= "Submit" value= "Submission" >
</div>
</form>
Servlet
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.setcharacterencoding ("Utf-8");
String method = Request.getparameter ("method");
if ("Toradio". Equals (method)) {
Get the values in the form
Toradio (request, response);
}else if ("Tosubmit". Equals (method)) {
Get the values in the form to be stored in the database
Tosubmit (request, response);
}
}
private void Tosubmit (HttpServletRequest request,
HttpServletResponse response) {
TODO auto-generated Method Stub
Radio r = GETFZ (request);
Iradioservice is=new Radioserviceimpl ();
int Ret=is.add (R);
if (ret>0) {
SYSTEM.OUT.PRINTLN ("added successfully! ");
}
}
private void Toradio (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
Radio r = GETFZ (request);
if (r! = null) {
Request.setattribute ("list", r);
Request.getrequestdispatcher ("confirm.jsp"). Forward (Request,
Response);
}
}
The value of the package
Private Radio GETFZ (HttpServletRequest request) {
int sex = Integer.parseint (Request.getparameter ("Sex"));
String w = request.getparameter ("w");
String s = request.getparameter ("s");
String C = request.getparameter ("C");
Radio r = new Radio ();
if (w = = null) {
W = "";
}
if (s = = null) {
s = "";
}
if (c = = null) {
c = "";
}
R.setsex (Sex);
R.SETHB (w + s + C);
System.out.println ("R:" + R);
return R;
}
dao//Data Access Layer
public int Add (Radio Radio) {
Sql= "INSERT into Test (SEX,HB) VALUES (?,?)";
Conn=db.getconn ();
int ret=0;
try {
Conn.setautocommit (FALSE);
Ps=conn.preparestatement (SQL);
Ps.setint (1, Radio.getsex ());
Ps.setstring (2, RADIO.GETHB ());
ret = Ps.executeupdate ();
if (ret>0) {
Conn.commit ();
return ret;
}else{
Conn.rollback ();
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
DB.CLOSECONN (Conn, PS, RS);
}
return ret;
}
Entity class
private int sex;
Private String HB;
Production Getset method
service//Operation class
public class Radioserviceimpl implements Iradioservice {
Radiodao r=new Radiodao ();
@Override
public int Add (Radio Radio) {
TODO auto-generated Method Stub
Return R.add (radio);
}
}
Db
Final static String URL = "Jdbc:mysql://localhost:3306/radio?userunicode=true&characterencoding=utf-8";
Final static String Dbdriver = "Com.mysql.jdbc.Driver";
Final static String user = "root";
Final static String password = "";
public static Connection Getconn () {
Connection Conn=null;
try {
Class.forName (Dbdriver);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
conn = drivermanager.getconnection (URL, user, password);
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return conn;
}
public static void Closeconn (Connection conn, PreparedStatement PS, ResultSet rs) {
try {
if (rs! = null) {
Rs.close ();
}
if (PS! = null) {
Ps.close ();
}
IF (conn! = null) {
Conn.close ();
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Jsp+servlet values for radio buttons and check boxes and stores them in the database