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:
@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")%>
...
The browser:
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.
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?
new Servlet doGet method:
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: ...
${generatedResponse}
...
Chrome: