Java servlet将空值打印到jsp
我在jsp中有一个web表单,它应该从servlet获取值,但它打印出空值。下面是jsp和servlet的代码。有人能告诉我如何修复下面的代码,以便它从请求对象打印出值,而不是打印空值吗 以下是my.jsp的代码:Java servlet将空值打印到jsp,java,jsp,servlets,Java,Jsp,Servlets,我在jsp中有一个web表单,它应该从servlet获取值,但它打印出空值。下面是jsp和servlet的代码。有人能告诉我如何修复下面的代码,以便它从请求对象打印出值,而不是打印空值吗 以下是my.jsp的代码: <jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" /> <form method="post"> <table>
<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>
tha:
最小值:
最大值:
下面是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;
}
}
公共类PlotPolarServlet扩展了HttpServlet{
私有请求调度程序jsp;
public void init(ServletConfig config)抛出ServletException{
ServletContext=config.getServletContext();
jsp=context.getRequestDispatcher(“/WEB-INF/jsp/my.jsp”);
}
受保护的无效数据集(HttpServletRequest-req、HttpServletResponse-resp)
抛出ServletException、IOException{
转发(请求、响应);
}
受保护的void doPost(HttpServletRequest-req、HttpServletResponse-resp)
抛出ServletException、IOException{
映射错误=验证(req);
如果(!errors.isEmpty()){
转发(请求、响应);
返回;
}
分别发送重定向(“我的”);
}
公共静态映射验证(HttpServletRequest请求){
HashMap errors=新建HashMap();
请求setAttribute(“错误”,错误);
字符串tha=req.getParameter(“tha”);
如果(tha==null | | tha.trim().length()==0){
错误。输入(“tha”,“tha required.”);
}
字符串min=req.getParameter(“min”);
if(min==null | | min.trim().length()==0){
错误。输入(“最小”,“需要最小”);
}
字符串max=req.getParameter(“max”);
如果(max==null | | max.trim().length()==0){
错误。输入(“最大”、“所需最大”);
}
返回错误;
}
}
我最初误解了你的问题。问题在于参数输入是否正确,而不是出现错误。此代码的else
部分
<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}
else{out.println(request.getParameter("min"));}%>
这被称为flash scope和flash属性,可以通过servlet过滤器实现。基本上,在两个请求之间存储要重用的属性,然后删除它们
至于编辑:
除非您在编辑中复制粘贴了错误的代码
<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>
不yX
。因此,else
将被计算,并且参数存在,因为它是相同的请求
更多解释
在您的my
示例中:
如果未输入必填字段,将调用doPost()
方法,填充errors
映射,并且代码执行jsp.forward(req,resp)代码>,在呈现jsp时使用相同的请求和参数
如果您输入了所有必填字段,将调用doPost()
,并响应sendRedirect(“我的”)代码>被执行。这会导致浏览器发送带有新参数/属性的新GET HTTP请求。这将导致调用doGet()
来处理新请求,该请求将转发给jsp。原始请求参数不包括在此请求中,因此没有任何显示,因此当呈现else{out.println(request.getParameter(“min”);
时,null
在您的myother
示例中,由于jsp中的
参数名与您在servlet中查找的参数名之间存在差异,您得到的结果与我上面解释的完全无关。忽略此servlet。它没有做您认为正确的事情。Sidenote:Get去掉那个实例变量,并在JSP中使用JSTL/EL!@那么你能更具体一点吗?建议实际行若要添加/删除?@code对所有非JSTL/EL的代码进行了编码。您是否验证了验证
是否正在运行?@code对您的代码进行了编码您的代码按预期工作,我已更新了我的答案以解释发生了什么。我最初误解了您从哪里获得null
。但是,问题仍然来自发送重定向
。感谢您的帮助让我知道我错了:).Upvote for correct response:)@PrasadKharkar我也会发表评论,但你在我之前就删除了。@CodeMed在你的jsp:useBean
中更改范围。该属性现在在会话中。不要在jsp中使用Java代码,这是一种不好的做法。@CodeMed你会将其更改为session
errors
属性现在将在会话中。您也可以将${errors}
与一起使用。EL处理器先查看请求属性,然后查看会话属性,然后查看应用程序属性,返回它找到的第一个属性。@code问题与我在回答中描述的一样。您正在使用resp.sendRedirect(“my”);
。在您的另一个项目中,您可能正在使用RequestDispatcher#forward(req,resp)
。它们有不同的用途。
<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>
errors.put("imageParam1", "imageParam1 required.");
...
if (!errors.isEmpty()){
jsp.forward(req, resp);
return;
}