jsp+servlet對於選項按鈕和複選框取值並且存放到資料庫中

來源:互聯網
上載者:User

標籤: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對於選項按鈕和複選框取值並且存放到資料庫中

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.