简体   繁体   English

servlet将空值打印到jsp

[英]servlet printing null values to jsp

I have a web form in a jsp that is supposed to get values from a servlet, but it is printing out null values. 我在jsp中有一个Web表单,它应该从servlet获取值,但它打印出空值。 I am including the code for the jsp and for the servlet below. 我包含了jsp和下面的servlet的代码。 Can anyone show me how to fix the code below so that it prints out the values from the request object instead of printing null? 任何人都可以告诉我如何修复下面的代码,以便它打印出请求对象的值而不是打印null?

Here is the code for my.jsp: 这是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>  

And here is the code for the servlet: 这是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;
    }
}  

I originally misread your question. 我原本误读了你的问题。 The issue is when parameters are input correctly, not when there is an error. 问题是何时正确输入参数,而不是在出现错误时。 The else part of this code 这段代码的else部分

<% if (errors.containsKey("min")) {out.println("<span class=\"error\">" + errors.get("min") + "</span>");}  
else{out.println(request.getParameter("min"));}%>  

can't print anything but null because, in this request, there are no parameters with those keys. 不能打印除null任何内容,因为在此请求中,没有带这些键的参数。

A HttpServletResponse#sendRedirect(String) returns a 302 HTTP status code which would make your browser send a new HTTP request to the location described by the String parameter. HttpServletResponse#sendRedirect(String)返回302 HTTP状态代码,该代码将使您的浏览器向String参数描述的位置发送新的HTTP请求。 The request parameters would not be in the new request (the request context/scope is cleared once the request is handled). 请求参数不在新请求中(一旦处理请求就清除请求上下文/范围)。 You would need to put them in the session attributes. 您需要将它们放在会话属性中。

/*
   for every parameter, put it in the session attributes
*/
req.getSession(true).setAttribute("myParam", req.getParameter("myParam"));
resp.sendRedirect("my");

This is known as flash scope and flash attributes and it can be implemented as explained here with a servlet Filter . 这就是所谓的闪光范围和闪光灯的属性,它可以作为解释的实施在这里使用Servlet Filter You basically store attributes you want to reuse between 2 requests, then delete them. 您基本上存储要在2个请求之间重用的属性,然后删除它们。

As for Edit: 至于编辑:

Unless you've copy-pasted the code in your edit incorrectly 除非您在编辑中错误地复制粘贴代码

<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>

you're POST ing a parameter called yX , but looking for imageParam . POST荷兰国际集团名为参数yX ,但寻找imageParam Your doPost() method will create an errors request attribute and then forward (instead of sendRedirect ). 您的doPost()方法将创建errors请求属性,然后forward (而不是sendRedirect )。

errors.put("imageParam1", "imageParam1 required.");
...
if (!errors.isEmpty()){
    jsp.forward(req, resp);
    return;
}

not yX . 不是yX Therefore, the else gets evaluated and the parameter exists since it's the same request. 因此, else被评估并且参数存在,因为它是相同的请求。

MORE EXPLANATION 更多解释

In your my example: 在你my例子:

  1. If you didn't enter a required field, the doPost() method gets called, the errors map is populated and your code does jsp.forward(req, resp); 如果没有输入必填字段,则调用doPost()方法,填充errors映射,并且代码执行jsp.forward(req, resp); which uses the same request and the parameters are available when the jsp is rendered. 它使用相同的请求,并且在呈现jsp时可以使用参数。
  2. If you entered all the required fields, the doPost() is called and resp.sendRedirect("my"); 如果输入了所有必填字段,则调用doPost()并调用resp.sendRedirect("my"); is executed. 被执行。 This causes your browser to send a new GET HTTP request with new parameters/attributes. 这会导致您的浏览器使用新参数/属性发送新的GET HTTP请求。 This causes the doGet() to be called to process the new request, which forwards to your jsp. 这会导致调用doGet()来处理新请求,该请求将转发到您的jsp。 The original request parameters aren't included in this request so there is nothing to show, thus null when else{out.println(request.getParameter("min")); 原始请求参数不包含在此请求中,因此无需显示任何内容,因此在else{out.println(request.getParameter("min"));时为null else{out.println(request.getParameter("min")); gets rendered. 得到了。

In your myother example, because of the difference between the <input> parameter name in the jsp and the parameter name you're looking for in the servlet, you're getting completely irrelevant results which I've explained above. 在你myother例如,由于之间的差别<input>在jsp参数名称和你正在寻找在servlet,你要我在前面已经解释完全不相关的结果参数名称。 Disregard this servlet. 忽略这个servlet。 It is not doing what you thought it was doing correctly. 它没有做你认为正确做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM