【问题标题】:JSP Servlet does not work after using IF block使用 IF 块后 JSP Servlet 不起作用
【发布时间】:2015-08-14 14:39:06
【问题描述】:

这个 servlet 在没有使用 if 块时可以正常工作,但现在我需要使用 if 块添加更多选项。 请问谁能告诉我这是为什么?

    public class SearchServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");

        FileReader fr = null;
        BufferedReader br = null;

        try {

            ArrayList<Contact> contactList = new ArrayList<>();

            String query = request.getParameter("q");

            String path = getServletContext().getRealPath("pb.txt");
            File file = new File(path);
            fr = new FileReader(file);
            br = new BufferedReader(fr);

            String byPhone = request.getParameter("byPhone");
            String byAddress = request.getParameter("byAddress");
            String byEmail = request.getParameter("byEmail");

            out.println(byPhone);

            String data = null;

            if (byPhone.equals("on")) {
                out.print("By Phone:"+byPhone);
                while ((data = br.readLine()) != null) {
                    String[] token = data.split(":");
                    if (token[1].toLowerCase().startsWith(query.toLowerCase())) {
                        Contact c = new Contact(token[0], token[1], token[2], token[3]);
                        contactList.add(c);
                    }
                }
            }


                out.print("Else");
                while ((data = br.readLine()) != null) {
                    String[] token = data.split(":");
                    if (token[0].toLowerCase().startsWith(query.toLowerCase())) {
                        Contact c = new Contact(token[0], token[1], token[2], token[3]);
                        contactList.add(c);
                    }
                }


            out.print("<h1>Results</h1>");

            out.print("<table>");
            out.print("<tr>");
            out.print("<td>Name</td>");
            out.print("<td>Phone No</td>");
            out.print("<td>Email</td>");
            out.print("<td>Address</td>");
            out.print("</tr>");
            for (Contact c : contactList) {
                out.print("<tr>");
                out.print("<td>" + c.getName() + "</td>");
                out.print("<td>" + c.getPhoneNo() + "</td>");
                out.print("<td>" + c.getEmail() + "</td>");
                out.print("<td>" + c.getAddress() + "</td>");
                out.print("</tr>");
            }

            out.print("</table>");
            out.print("<a href=\"search.html\">Back</a>");

        } finally {
            if (fr != null) {
                fr.close();
            }
            if (br != null) {
                br.close();
            }
            out.close();
        }
    }
}

您可以在 String data = null; 正下方看到 if 块;

谢谢!

【问题讨论】:

  • 它在什么方面不起作用?
  • @Andreas 没有 if(byPhone.equals("on")){ } 块
  • 你说它“不起作用”,但你并没有说什么是错的。它会产生异常吗?它会产生意想不到的结果吗?还有什么?
  • @Andreas 是的,它给出了空指针异常!

标签: javascript java jsp servlets


【解决方案1】:

它可能失败的一个明显方式似乎是在请求中未提供参数byPhone,留下变量byPhone = null,导致if语句出现NullPointerException

两种解决方法:

// Explicit null check
if (byPhone != null && byPhone.equals("on")) {

// Reverse equals check
if ("on".equals(byPhone)) {

【讨论】:

  • 哦,是的,您可能希望在 if 语句中使用 else 子句,围绕第二个 while 循环。
  • 它使用第一个 if 语句工作非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 2014-01-22
  • 1970-01-01
相关资源
最近更新 更多