日期:2014-05-20  浏览次数:20848 次

想在servlet中保存一段String,然后在JSP页面进行输出
Java code

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String searchName= new String(request.getParameter("txtPname").getBytes("ISO8859-1"),"UTF-8");
        ProductDao productDao = new ProductDao();
        try {
            ArrayList<Product> productList = productDao.search(searchName);            
            HttpSession session = request.getSession();
            session.setAttribute("productList", productList);
            if(productList.isEmpty()){
                productList = null;
                productList=productDao.selectAll();
                session= request.getSession();
                session.setAttribute("productList", productList);                
                request.getRequestDispatcher("/ProductList.jsp").forward(request, response);    
                //想在这里加入输出到页面的String
                
            }else{
                request.getRequestDispatcher("/ProductList.jsp").forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



------解决方案--------------------
request.getRequestDispatcher("/ProductList.jsp").forward(request, response); 
这个已经跳转了、在跳转前加不行吗?
------解决方案--------------------
上面说的有道理,把值保存在attritute里面然后在页面上取出来,打印出来就好了