jsp+servlet对于单选按钮和复选框取值并且存放到数据库中
Posted 等一串神秘数字
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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对于单选按钮和复选框取值并且存放到数据库中的主要内容,如果未能解决你的问题,请参考以下文章