1111111111111111

jsp+servlet对于单选按钮和复选框取值并且存放到数据库中

index.jsp

<form action="index.gj?method=toradio" method="post">
<div align="center">
<h1>请选择</h1>
性别:<input type="radio" name="sex" value="1">男
<input type="radio" name="sex" value="0">女
<br>
爱好:<input type="checkbox" name="w" value="玩">玩
<input type="checkbox" name="s" value="睡">睡
<input type="checkbox" name="c" value="吃">吃
<br>
<input type="submit" value="提交">
</div>
</form>

confirm.jsp

<form action="index.gj?method=tosubmit" method="post">
<div align="center">
<br/><br/>
性别:<input type="radio" name="sex" value="1"
${(list.sex)==1?"checked":""} />男 <input
type="radio" name="sex" value="0"
${(list.sex)==0?"checked":""} />女
<br>
爱好: <input type="checkbox" name="w" value="玩" <c:if test="${list.hb.contains('玩')}">checked="checked" </c:if> >玩
<input type="checkbox" name="s" value="睡" <c:if test="${list.hb.contains('睡')}">checked="checked" </c:if> >睡
<input type="checkbox" name="c" value="吃"<c:if test="${list.hb.contains('吃')}">checked="checked" </c:if> >吃
<input type="submit" value="提交">
</div>
</form>

 

 

String method = request.getParameter("method");

if ("toradio".equals(method)) {

//获取表单中的值
toradio(request, response);
}else if("tosubmit".equals(method)){

//获取到表单中的值存进数据库
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("添加成功!");
}

}

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);
}
}

 

//取值的封装

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;
}

 

posted @ 2017-09-18 11:52  等一串神秘数字  阅读(3909)  评论(0编辑  收藏  举报