[英]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.