標籤:pos encoding 選擇 checkbox cut contains values row character
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對於選項按鈕和複選框取值並且存放到資料庫中