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>

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)) {

//获取表单中的值
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;
}

dao//数据访问层

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

实体类

private int sex;
private String hb;

生产getset方法

Service//操作类

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对于单选按钮和复选框取值并且存放到数据库中的更多相关文章

  1. jquery,js,checkbox多选框复选框取值和赋值

    今天一个同事不太会多选框的取值和赋值的问题,我帮他解决了一下,不想自己想的朋友可以参考一下. 获取checkBox的值,checkBox的html如下 <input type=" na ...

  2. input checkbox复选框取值

    <table> <!--列表表头 开始 --> <tr class="ui-widget ui-state-hover" style="he ...

  3. ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)

    这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...

  4. JAVA 单选按钮、复选按钮

    //单选按钮和复选按钮 import java.awt.*; import javax.swing.*; public class Jiemian6 extends JFrame{ JPanel mb ...

  5. 单选按钮、复选按钮——axure线框图部件库介绍

    有时候发现这做事情坚持下来是一件很不容易的,写教程也一样,不过听到很多朋友对我说 这个全部是图片的教程 对他们入门帮助很多,我就想想 在坚持坚持把基础部分先完善了! 1. 简单的问卷调查: 您的性别? ...

  6. 【特效】单选按钮和复选框的美化(只用css)

    表单的默认样式都是比较朴素的,实际页面中往往需要美化他们.这里先说说单选按钮和复选框,有了css3,这个问题就变的好解决了.利用input与label相关联,对label进行美化并使其覆盖掉原本的in ...

  7. php表单中如何获取单选按钮与复选按钮的值(示例)

    转载:http://www.php.cn/php-weizijiaocheng-360027.html php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个“checked”属 ...

  8. ExtJS中,将Grid表头中的全选复选框取消复选

    今天发现公司产品用的EXTJS中使用Grid时,Grid表头中的全选复选框的选中状态不是很准确,就写了这个小扩展 在js中加入下面方法,在需要取消全选的地方调用即可,例:Ext.getCmp('gri ...

  9. UI设计规范:单选按钮 vs 复选框,没那么简单

    无论是网页设计,还是移动app设计,都经常用到单选按钮和复选框这两个组件.这两个组件看似意义明确,很好区分,但在实际设计中却很容易用错,带来不好的用户体验. 本文中我通过列举几个典型的错误用法,帮助设 ...

随机推荐

  1. uploadify 在chrome上崩溃的解决办法

    使用Uploadify进行文件上传,Chrome经常会报“喔唷,崩溃啦”的错误,见下图: 很显然,这是Chrome浏览器缓存功能,所导致的问题.从LOG里也能看到:正常的情况下,会请求文件(jquer ...

  2. Linux-进程描述(2)之进程标识符进程位置与环境变量

    在上一篇文章中详细介绍了task_struct结构体内的常见成员,然后我们就来看一下具体内容.每个进程都把它的信息放在 task_struct 这个数据结构中,task_struct 包含了这些内容: ...

  3. webpack打包体积优化

    优化: 1:外部引入模块(cdn)     如 jquery,zepto,d3, bootstrap这些固定的lib 使用cdn直接引用就可以,没有必要打包到build,有效利用302. 2:图标优化 ...

  4. 原创:Docker在云家政的应用 谢绝复制粘贴内容

    我们公司目前大规模使用了Docker,目前除了数据库应用,其他所有应用都在Docker容器内运行,下面我就Docker在公司的应用做一些分享.. 首先我介绍一下公司的背景,公司属于中小型创业公司,服务 ...

  5. CORS跨域资源共享你该知道的事儿

    "唠嗑之前,一些客套话" CORS跨域资源共享,这个话题大家一定不陌生了,吃久了大转转公众号的深度技术好文,也该吃点儿小米粥溜溜胃里的缝儿了,今天咱们就再好好屡屡CORS跨域资源共 ...

  6. 关于批量插入数据之我见(100万级别的数据,mysql) (转)

    因前段时间去面试,问到如何高效向数据库插入10万条记录,之前没处理过类似问题,也没看过相关资料,结果没答上来,今天就查了些资料,总结出三种方法: 测试数据库为MySQL!!! 方法一: public  ...

  7. nmon 使用

    官网:http://nmon.sourceforge.net/pmwiki.php 步骤 1. 下载nmon到linux上: 2. 解压tar包,根据使用的linux版本选择使用的文件,并对其加上使用 ...

  8. MarkDown---超强文本编辑器

    What you see Is What you  get ... --------------------------- Salmon 编辑器界面: ------------------------ ...

  9. VMware Workstation 12 Pro 之安装林耐斯-Solus-系统

    VMware Workstation 12 Pro 之安装林耐斯-Solus-系统... ----------------- ----------------- ------------------- ...

  10. Akka(22): Stream:实时操控:动态管道连接-MergeHub,BroadcastHub and PartitionHub

    在现实中我们会经常遇到这样的场景:有一个固定的数据源Source,我们希望按照程序运行状态来接驳任意数量的下游接收方subscriber.又或者我需要在程序运行时(runtime)把多个数据流向某个固 ...