如何正确地将数据从Servlet传递到JSP?

时间:2022-08-19 11:49:20

I need to pass data from Servlet (which it gets from DAO) to JSP. The wrong data ends up in JSP (with overhead of html code from Servlet). How it ends up there? How to correctly handle this situation?

我需要将数据从Servlet(它从DAO获取)传递给JSP。错误的数据最终在JSP中(带有来自Servlet的html代码的开销)。它到底是怎么结束的?如何正确处理这种情况?

Servlet:

@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

String reqId = request.getParameter("id");
        Integer reqIdInt = null;
        String tempHTML = "";

        try {
            reqIdInt = Integer.parseInt(reqId);
            } catch (NumberFormatException e) {    }
        /**
         * findById()
         */
        if (reqIdInt != null) {
            // s?id=.....
            DSLR dslr = dslrDAO.findById(reqIdInt);

            if(dslr != null){
                tempHTML ="<form action=\"s?action=save\" method=\"POST\">\n" +
                        "\n" +
                        "<input type=\"hidden\" name=\"id\" value=\""+dslr.getDslrId()+"\">\n" +
                        "\n" +
                        "\n" +
                        "<table bgcolor=\"#000000\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><table  border=\"0\" cellpadding=\"2\" cellspacing=\"1\">\n" +
                        "<tbody><tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">id:</td><td>"+dslr.getDslrId()+"</td></tr>\n" +
                        "<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">model:</td><td>&nbsp;<b><font color=\"#0000ff\"><input type=\"text\" value=\""+dslr.getModel()+"\" name=\"model\"></font></b></td></tr>\n" +
                        "<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">price:</td><td>&nbsp;<b><font color=\"#0000ff\"><input type=\"text\" value=\""+dslr.getPrice()+"\" name=\"price\"></font></b></td></tr>\n" +
                        "<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td>&nbsp;<b><font color=\"#0000ff\"><textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</font></b></td></tr>\n" +
                        "</tbody></table></td></tr></tbody></table>\n" +
                        "<input type=\"submit\" value=\"savedata\">\n" +
                        "</form>" +
                        "<tr><td>" + dslr.getDslrId() +
                        "</td><td>" + dslr.getModel() +
                        "</td><td>" + dslr.getPrice() +
                        "</td><td><a href=\"javascript:get_dslr(" + dslr.getDslrId() + ") target=\"_self\"\">" + dslr.getDescription() +
                        "</td><td><a href=\"" + "s?id=" + dslr.getDslrId() + "\">modify</a></td></tr>";

//                request.getSession().setAttribute("generatedResponse",tempHTML);
                request.setAttribute("generatedResponse",tempHTML);
                request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
            }
}

JSP

...

<%=(String)request.getAttribute("generatedResponse")%>
...

The browser:

如何正确地将数据从Servlet传递到JSP?


UPDATE:

Screenshot shows the excessive HTML (which is somehow dragged from servlet or jsp and put in description text field, for example: </html>). The form data which is wrong is highlighted (yellow) for clarity I don't need to customize font of text area.

屏幕截图显示了过多的HTML(以某种方式从servlet或jsp拖动并放入描述文本字段,例如: )。为了清楚起见,突出显示错误的表单数据(黄色)我不需要自定义文本区域的字体。


UPDATE2:

I managed to handle messy code in Servlet but I still don't like to store HTML in Servlet. I'd like to have there only logic. I don't want to put HTML generation directly to JSP. How to separate logic from HTml in this example?

我设法在Servlet中处理凌乱的代码,但我仍然不喜欢在Servlet中存储HTML。我想只有逻辑。我不想将HTML生成直接放到JSP上。在这个例子中如何将逻辑与HTml分开?

new Servlet doGet method:

新的Servlet doGet方法:

    String reqId = request.getParameter("id");
    Integer reqIdInt = null;
    String tempHTML = "";

    try {
        reqIdInt = Integer.parseInt(reqId);
        } catch (NumberFormatException e) {    }
    /**
     * findById()
     */
    if (reqIdInt != null) {
        // s?id=.....
        DSLR dslr = dslrDAO.findById(reqIdInt);

        if(dslr != null){
            tempHTML = "<form action=\"s?action=save\" method=\"POST\">\n" +
                    "<input type=\"hidden\" name=\"id\"  value=\""+dslr.getDslrId()+"\">\n" +
                    "\t<table class=\"table\" bgcolor=\"#000000\" border=\"0\">\n" +
                    "\t\t<tr>\n" +
                    "\t\t\t<td>id:</td>\n" +
                    "\t\t\t<td>"+dslr.getDslrId()+"</td>\n" +
                    "\t\t</tr>\n" +
                    "\t\t<tr>\n" +
                    "\t\t\t<td>model:</td>\n" +
                    "\t\t\t<td><input type=\"text\" name=\"dslr_model\" value=\""+dslr.getModel()+"\"></td>\n" +
                    "\t\t</tr>\n" +
                    "\t\t<tr>\n" +
                    "\t\t\t<td>price:</td>\n" +
                    "\t\t\t<td><input type=\"text\" name=\"dslr_price\" value=\""+dslr.getPrice()+"\"></td>\n" +
                    "\t\t</tr>\n" +
                    "\t\t<tr>\n" +
                    "\t\t\t<td>description:</td>\n" +
                    "\t\t\t<td><textarea name=\"dslr_description\" cols=\"30\" rows=\"10\">"+dslr.getDescription()+"</textarea></td>\n" +
                    "\t\t</tr>\n" +
                    "\t</table>\n" +
                    "</form>\n" +
                    "<input type=\"submit\" value=\"saveform\">" ;

            request.getSession().setAttribute("generatedResponse",tempHTML);
            request.setAttribute("generatedResponse",tempHTML);
            request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
        }else {
            // SQLException
        }

        /**
         *  findAll()
          */
    } else {

        List<DSLR> dslrs = dslrDAO.findAll();
        if (dslrs != null) {
            tempHTML = "<table class=\"table\" bgcolor=\"#000000\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" +
                    "    <tr bgcolor=\"#FFFFFF\" nowrap=\"\">\n" +
                    "        <td>Id</td>\n" +
                    "        <td>Model</td>\n" +
                    "        <td>Price</td>\n" +
                    "    </tr>\n" ;
            for (Iterator<DSLR> dslrIterator = dslrs.iterator(); dslrIterator.hasNext(); ) {
                DSLR next = dslrIterator.next();
                tempHTML += "<tr><td>" + next.getDslrId() +
                        "</td><td>" + next.getModel() +
                        "</td><td>" + next.getPrice() +
                        "</td><td><a href=\"javascript:get_dslr(" + next.getDslrId() + ") target=\"_self\"\">description " +
                        "</td><td><a href=\"" + "s?id=" + next.getDslrId() + "\">modify</a></td></tr>";
            }
            tempHTML += "</table>";

            request.getSession().setAttribute("generatedResponse",tempHTML);
            request.setAttribute("generatedResponse",tempHTML);
            request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
        }else {
            //SQLException
        }
    }

new JSP: ...

新JSP:...

${generatedResponse}

...

Chrome:

如何正确地将数据从Servlet传递到JSP?

1 个解决方案

#1


0  

You can not customize the font of textarea. So make it like this..

您无法自定义textarea的字体。所以就这样吧..

<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td>&nbsp;<textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</td></tr>

#1


0  

You can not customize the font of textarea. So make it like this..

您无法自定义textarea的字体。所以就这样吧..

<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td>&nbsp;<textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</td></tr>