[英]servlet printing null values to jsp
我在jsp中有一個Web表單,它應該從servlet獲取值,但它打印出空值。 我包含了jsp和下面的servlet的代碼。 任何人都可以告訴我如何修復下面的代碼,以便它打印出請求對象的值而不是打印null?
這是my.jsp的代碼:
<jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" />
<form method="post">
<table>
<tr>
<td width=302>
</td>
<td width=250>
<table>
<tr>
<td width=150 align="right">tha: </td>
<td><input type="text" name="tha" value="c3t" size="15" />
<%if (errors.containsKey("tha")) {out.println("<span class=\"error\">" + errors.get("tha") + "</span>");}
else{out.println(request.getParameter("tha"));}%>
</td>
</tr>
<tr>
<td width=150 align="right">min: </td>
<td><input type="text" name="min" value="0" size="15" />
<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}
else{out.println(request.getParameter("min"));}%>
</td>
</tr>
<tr>
<td width=150 align="right">max: </td>
<td><input type="text" name="max" value="2*pi" size="15" />
<% if (errors.containsKey("max")) {out.println("<span class=\"error\">" + errors.get("max") + "</span>");}
else{out.println(request.getParameter("max"));}%>
</td>
</tr>
<tr>
<td></td>
<td align="right">
<input type="submit" name="submit-button" value="Click To Plot" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
這是servlet的代碼:
public class PlotPolarServlet extends HttpServlet{
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/my.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
jsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
Map<String, String> errors = validate(req);
if (!errors.isEmpty()){
jsp.forward(req, resp);
return;
}
resp.sendRedirect("my");
}
public static Map<String, String> validate(HttpServletRequest req){
HashMap<String, String> errors = new HashMap<String, String>();
req.setAttribute("errors", errors);
String tha = req.getParameter("tha");
if (tha == null || tha.trim().length() == 0){
errors.put("tha", "tha required.");
}
String min = req.getParameter("min");
if (min == null || min.trim().length() == 0){
errors.put("min", "min required.");
}
String max = req.getParameter("max");
if (max == null || max.trim().length() == 0){
errors.put("max", "max required.");
}
return errors;
}
}
我原本誤讀了你的問題。 問題是何時正確輸入參數,而不是在出現錯誤時。 這段代碼的else
部分
<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}
else{out.println(request.getParameter("min"));}%>
不能打印除null
任何內容,因為在此請求中,沒有帶這些鍵的參數。
HttpServletResponse#sendRedirect(String)
返回302 HTTP狀態代碼,該代碼將使您的瀏覽器向String參數描述的位置發送新的HTTP請求。 請求參數不在新請求中(一旦處理請求就清除請求上下文/范圍)。 您需要將它們放在會話屬性中。
/*
for every parameter, put it in the session attributes
*/
req.getSession(true).setAttribute("myParam", req.getParameter("myParam"));
resp.sendRedirect("my");
這就是所謂的閃光范圍和閃光燈的屬性,它可以作為解釋的實施在這里使用Servlet Filter
。 您基本上存儲要在2個請求之間重用的屬性,然后刪除它們。
至於編輯:
除非您在編輯中錯誤地復制粘貼代碼
<td><input type="text" name="yX" value="cx" size="15" />
<%if (errors.containsKey("yX")) { // errors doesn't contain yX as a Key, it contains imageParam1
out.println("<span class=\"error\">" + errors.get("yX") + "</span>");
}else{out.println(request.getParameter("yX"));}%> // will print the value of the request parameter with key yX
</td>
你POST
荷蘭國際集團名為參數yX
,但尋找imageParam
。 您的doPost()
方法將創建errors
請求屬性,然后forward
(而不是sendRedirect
)。
errors.put("imageParam1", "imageParam1 required.");
...
if (!errors.isEmpty()){
jsp.forward(req, resp);
return;
}
不是yX
。 因此, else
被評估並且參數存在,因為它是相同的請求。
更多解釋
在你my
例子:
doPost()
方法,填充errors
映射,並且代碼執行jsp.forward(req, resp);
它使用相同的請求,並且在呈現jsp時可以使用參數。 doPost()
並調用resp.sendRedirect("my");
被執行。 這會導致您的瀏覽器使用新參數/屬性發送新的GET HTTP請求。 這會導致調用doGet()
來處理新請求,該請求將轉發到您的jsp。 原始請求參數不包含在此請求中,因此無需顯示任何內容,因此在else{out.println(request.getParameter("min"));
時為null
else{out.println(request.getParameter("min"));
得到了。 在你myother
例如,由於之間的差別<input>
在jsp參數名稱和你正在尋找在servlet,你要我在前面已經解釋完全不相關的結果參數名稱。 忽略這個servlet。 它沒有做你認為正確做的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.