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>
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对于单选按钮和复选框取值并且存放到数据库中的更多相关文章
- jquery,js,checkbox多选框复选框取值和赋值
今天一个同事不太会多选框的取值和赋值的问题,我帮他解决了一下,不想自己想的朋友可以参考一下. 获取checkBox的值,checkBox的html如下 <input type=" na ...
- input checkbox复选框取值
<table> <!--列表表头 开始 --> <tr class="ui-widget ui-state-hover" style="he ...
- ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)
这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...
- JAVA 单选按钮、复选按钮
//单选按钮和复选按钮 import java.awt.*; import javax.swing.*; public class Jiemian6 extends JFrame{ JPanel mb ...
- 单选按钮、复选按钮——axure线框图部件库介绍
有时候发现这做事情坚持下来是一件很不容易的,写教程也一样,不过听到很多朋友对我说 这个全部是图片的教程 对他们入门帮助很多,我就想想 在坚持坚持把基础部分先完善了! 1. 简单的问卷调查: 您的性别? ...
- 【特效】单选按钮和复选框的美化(只用css)
表单的默认样式都是比较朴素的,实际页面中往往需要美化他们.这里先说说单选按钮和复选框,有了css3,这个问题就变的好解决了.利用input与label相关联,对label进行美化并使其覆盖掉原本的in ...
- php表单中如何获取单选按钮与复选按钮的值(示例)
转载:http://www.php.cn/php-weizijiaocheng-360027.html php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个“checked”属 ...
- ExtJS中,将Grid表头中的全选复选框取消复选
今天发现公司产品用的EXTJS中使用Grid时,Grid表头中的全选复选框的选中状态不是很准确,就写了这个小扩展 在js中加入下面方法,在需要取消全选的地方调用即可,例:Ext.getCmp('gri ...
- UI设计规范:单选按钮 vs 复选框,没那么简单
无论是网页设计,还是移动app设计,都经常用到单选按钮和复选框这两个组件.这两个组件看似意义明确,很好区分,但在实际设计中却很容易用错,带来不好的用户体验. 本文中我通过列举几个典型的错误用法,帮助设 ...
随机推荐
- python 如何在一个for循环中遍历两个列表
是我在看<笨方法学python>过程中发现有一行代码看不懂--" for sentence in snippet, phrase:",所以研究了半天,感觉挺有收获的.所 ...
- 像Linux终端一样使用windows命令行【cmder】
像Linux终端一样使用windows命令行[cmder] 下载cmder 我下载的是full版,下载之后是个压缩包,解压之后点击cmder.exe即可运行. 需要解决的几个问题 默认的是λ,当然还是 ...
- 使用requests模块爬虫
虽然干技术多年了,但从没有写过博客,想来甚是惭愧,本篇作为我博客的第一篇,也是测试篇.不为写的好,只为博诸君一眸而已. 使用python爬虫,有几个比较常用的,获取html_content的模块url ...
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析
一.example逻辑伪码 myRTSPClient附带3个example程序:simple_example.complete_example.common_example.后两个example都是从 ...
- Django导入外部js、css、图片等巨大的坑
初学Django,踩过最大的坑只有之一.至今任然不是很明白. 通常我们导入外部js等文件都是 <script src="myScript.js"></script ...
- openstack配置
参考林海峰老师的配置教程 http://blog.51cto.com/9161406/1839667
- koa/redux middleware系统解析
middleware 对于现有的一些框架比如koa,express,redux,都需要对数据流进行一些处理,比如koa,express的请求数据处理,包括json.stringify,logger,或 ...
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)
上一篇:Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1) 服务器版本 Ubuntu 16.04 LTS. 本 ...
- Varnsih调用多台后端主机
author:JevonWei 版权声明:原创作品 Varnsih调用多个后端主机 环境 Varnish 192.168.198.139 图片服务端 192.168.198.120 程序服务端 192 ...
- Windows下配置Nginx
一.今天是2017年1月18日.揣着一种也许叫做冲动的心情,决定以后每天都记录和回顾新涉及的技术,巩固已学到的知识,坚持. 二.回归到今天的技术主题,因为之前对于[反向代理]这种技术充满了好奇,借此机 ...