关于通过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;

    }

1个回答

             OutputStream outputStream = response.getOutputStream();//获取OutputStream输出流
            response.setHeader("content-type", "text/html;charset=UTF-8");//通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话
       byte[] dataByteArr = monsterlist.getBytes("UTF-8");//将字符转换成字节数组,指定以UTF-8编码进行转换
       outputStream.write(dataByteArr);//使用OutputStream流向客户端输出字节数组

如果 monsterlist.getBytes不行 把 monsterlist转成json 再试

不过jsp技术有点老了 建议你用ajax 或者其他一些技术方案

hqd66
朝目标努力的苍白 行,我试试,谢谢您
9 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问