🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
CategoryServlet.java ~~~ package zyw.web; import org.apache.commons.beanutils.BeanUtils; import zyw.bean.Category; import zyw.service.CategoryService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.Date; import java.util.Map; @WebServlet(name = "CategoryServlet",urlPatterns = "/category") public class CategoryServlet extends BaseServlet { public void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取参数 通过BeanUtils封装实体类 try { Map<String, String[]> parameterMap = request.getParameterMap(); Category category = new Category(); category.setCreatetime(new Date()); BeanUtils.populate(category,parameterMap); CategoryService categoryService=new CategoryService(); boolean b = categoryService.addCategory(category); if (b){ //添加成功 response.setStatus(201); request.getRequestDispatcher("/category-add.jsp").forward(request,response); }else { // 添加失败 response.setStatus(600); request.getRequestDispatcher("/category-add.jsp").forward(request,response); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } } } ~~~ CategoryService.java ~~~ package zyw.service; import zyw.bean.Category; import zyw.dao.CategoryDao; import java.sql.SQLException; public class CategoryService { //添加 public boolean addCategory(Category category) throws SQLException { CategoryDao dao = new CategoryDao(); boolean addCategory = dao.addCategory(category); return addCategory; } } ~~~ CategoryDao.java ~~~ package zyw.dao; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import zyw.bean.Category; import java.sql.SQLException; public class CategoryDao { public boolean addCategory(Category category) throws SQLException { ComboPooledDataSource dataSource=new ComboPooledDataSource(); QueryRunner queryRunner=new QueryRunner(dataSource); String sql="insert into category values(null,?,?,?,?)"; int row = queryRunner.update(sql, category.getC_name(), category.getPlace(), category.getCreatetime(), category.getType()); if (row>0){ return true; }else { return false; } } } ~~~ category-add.jsp中重要部分 ![](https://box.kancloud.cn/d893f091495a16a598c729e024a10d33_587x110.png) ~~~ <% System.out.println(response.getStatus() + "--------------status"); if (response.getStatus() == 201) { out.write("<script type=\"text/javascript\">\n" + " window.onload=function(){\n" + " showdiv();\n" + " }</script>"); } else if (response.getStatus() == 200) { } else { out.write("<script type=\"text/javascript\">\n" + " window.onload=function(){\n" + " hidediv();\n" + " }</script>"); } %> <script> function showdiv() { document.getElementById('sucess-info').style.display = 'block';//show的display属性设置为block(显示) document.getElementById('fail-info').style.display = 'none';//show的display属性设置为block(显示) } function hidediv() { document.getElementById('fail-info').style.display = 'block'; document.getElementById('sucess-info').style.display = 'none';//show的display属性设置为none(隐藏) } </script> ~~~ 结果 ![](https://box.kancloud.cn/a976e9d0e6ee7c5b7fe82047d244b558_1330x457.png)