[英]How to correctly pass data from Servlet to JSP?
我需要將數據從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> <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> <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> <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")%>
...
瀏覽器:
更新:
屏幕截圖顯示了過多的HTML(以某種方式將其從servlet或jsp拖放到description
文本字段中,例如: </html>
)。 為清楚起見,錯誤的表單數據被突出顯示(黃色),我不需要自定義文本區域的字體。
UPDATE2:
我設法在Servlet中處理凌亂的代碼,但我仍然不喜歡在Servlet中存儲HTML。 我只想知道邏輯。 我不想將HTML生成直接放入JSP。 在此示例中,如何將邏輯與HTml分開?
新的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
}
}
新的JSP: ...
${generatedResponse}
...
鉻:
您不能自定義textarea
的字體。 所以就這樣吧
<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td> <textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</td></tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.