我想从 mysql 中检索复选框值,我以 1(选中)和 0(未选中)的形式将其存储,但我不知道如何检索它。否则是否有任何其他选项可用于相同
这是我的jsp和servlet代码:
爱好测试.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="HobbiesTestServlet" method="post">
<input type = "checkbox" name = "Hobbies" value = 'Dancing' >Dancing
<input type = "checkbox" name = "Hobbies" value = 'Reading'>Reading
<input type = "checkbox" name = "Hobbies" value = 'Singing' >Singing
<input type = "checkbox" name = "Hobbies" value = 'Programming'>Programming
<input type = "checkbox" name = "Hobbies" value = 'Sleeping'>Sleeping
<input type = "submit" value = 'Submit'>
</form>
</body>
</html>
爱好TestServlet.java
package com.fulcrum.EmpForm;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HobbiesTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HobbiesTestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int danceCheck = 0;
int singCheck = 0;
int readCheck = 0;
int programCheck = 0;
int sleepCheck = 0;
List<String> hobbiesList = new ArrayList<String>();
String[] hobbies = request.getParameterValues("Hobbies");
if(hobbies != null){
for(int i=0;i<hobbies.length;i++){
System.out.println("hobbies are"+hobbies[i]);
hobbiesList.add(hobbies[i]);
}
}
if(hobbiesList.contains("Dancing")){
danceCheck = 1;
}
if(hobbiesList.contains("Reading")){
readCheck = 1;
}
if(hobbiesList.contains("Singing")){
singCheck = 1;
}
if(hobbiesList.contains("Programming")){
programCheck = 1;
}
if(hobbiesList.contains("Sleeping")){
sleepCheck = 1;
}
System.out.println(danceCheck);
System.out.println(readCheck);
System.out.println(singCheck);
System.out.println(programCheck);
System.out.println(sleepCheck);
}
}
我的数据库有舞蹈、阅读、唱歌、编程、睡眠等列...这里我没有包含数据库插入代码,但我知道它会起作用..只是想知道如何从数据库中检索那些值为 1 的列名并在jsp页面中显示给用户。