关于通过servlet查询jsp传过来的数据后显示在jsp页面的问题

本人写了个系统,经过测试以及能够运行到sql语句了,现在卡在不知道如何查询后的结果显示在jsp上,请教各位大神
,代码如下


servlet中的post方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String card = request.getParameter("card");
        String type = request.getParameter("type");
        String cardsearch = request.getParameter("cardsearch");

        CardSearchDAO cd = new CardSearchDAOImpl();
        List monsterlist;
        if(card.equals("monster")) {
            monsterlist = cd.getMonsetCardList(type, cardsearch);
            request.setAttribute("monsterlist", monsterlist);
            request.getRequestDispatcher("searchresult.jsp").forward(request, response);
        }
        if(card.equals("magic")) {
            cd.getMagicandtrapCardList(type, cardsearch);
        }
    }

DAOImpl中的getMonsterCardList方法

    public List getMonsterCardList(String type, String cardsearch) {

        StringBuffer sbf = new StringBuffer("select * from monstercard where 1=1");
        if(type!=null) {sbf.append(" and type='"+type+"'");}
        if(cardsearch!=null) {sbf.append(" and cardname='"+cardsearch+"'");}
        String sql = sbf.toString();
        System.out.println(sql);
        List monsterlist = new ArrayList();
        conn = DBConnection.getConnectDb();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                MonsterCard mc = new MonsterCard();
                mc.setCardname(rs.getString("cardname"));
                mc.setAtk(rs.getString("atk"));
                mc.setDef(rs.getString("def"));
                mc.setCardpack(rs.getString("cardpack"));
                mc.setEffect(rs.getString("effect"));
                mc.setQA(rs.getString("QA"));
                mc.setStauts(rs.getString("status"));
                mc.setType(rs.getString("type"));
                mc.setCardid(rs.getString("cardid"));
                monsterlist.add(mc);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            }

        return monsterlist;

    }
查看全部
hqd66
朝目标努力的苍白
2019/04/29 22:15
  • java
  • java-ee
  • 点赞
  • 收藏
  • 回答
    私信

1个回复