首页>代码>使用JSP+jqueryUI+java Servlet通过Apache POI实现Excel导入导出>/POIExcel/src/wk/servlet/UserJsonServlet.java
package wk.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; 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; import com.alibaba.fastjson.JSON; import wk.model.UserBean; import wk.util.DBTool; public class UserJsonServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //请求发送都是utf-8 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); List<UserBean> list = new ArrayList<UserBean>(); DBTool dbt = new DBTool(); PreparedStatement pstmt = null; Connection conn = null; ResultSet rs = null; String json = null; try { conn = dbt.getConnection(); String sql = "SELECT id,name,sex,age,tell,address FROM testexcel"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { UserBean ub = new UserBean(); ub.setId(rs.getInt("id")); ub.setName(rs.getString("name")); ub.setSex(rs.getString("sex")); ub.setAge(rs.getInt("age")); ub.setTell(rs.getInt("tell")); ub.setAddress(rs.getString("address")); list.add(ub); } json = JSON.toJSONString(list); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json); } catch (SQLException e) { e.printStackTrace(); } } }