编译错误<identifier> expected </identifier>

时间:2014-10-30 17:02:22

标签: java compiler-errors

所以它可能很简单,但我仍然无法自己解决这个问题。 这是什么意思?

GET.java:27: error: <identifier> expected
        out.println("</p></body></html>");
                   ^
GET.java:27: error: illegal start of type
        out.println("</p></body></html>");

这是我的java servlet的代码:

  import java.io.*;
  import java.util.*;
  import javax.servlet.*;
  import javax.servlet.http.*;

  public class GET extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
                      throws ServletException, IOException {
      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      FileOutputStream fos = new FileOutputStream("/tmp/prdocutorders.txt");
      PrintWriter pw = new PrintWriter(fos);
      out.println("<html><body><p>");
      pw.println("<h3>Congratulations! Your order information:</h3>");
      Enumeration e = req.getParameterNames();
      while ( e.hasMoreElements() ) {
        String name = (String)e.nextElement();
        String vals[] = (String[])req.getParameterValues(name);
        if ( vals != null )
          for (int i = 0; i < vals.length; i++)
            out.println(name + ": " + vals[i] + "<br>");
      }
       if ( vals != null )
          for (int i = 0; i < vals.length; i++)
            pw.println(name + ": " + vals[i] + "<br>");
      }
        out.println("</p></body></html>");
     // out.close();
    }

感谢您的帮助!!

2 个答案:

答案 0 :(得分:2)

平衡大括号强制性。你在声明out.println("</p></body></html>");

之前丢了大括号

作为旁注,通过

更容易发现错误
  • 始终使用大括号
  • 正确缩进代码

编辑:代码的某些部分似乎不正确,您可能需要这样:

while (e.hasMoreElements()) {
    String name = (String) e.nextElement();
    String vals[] = (String[]) req.getParameterValues(name);
    if (vals != null) {
        for (int i = 0; i < vals.length; i++) {
            out.println(name + ": " + vals[i] + "<br>");
            pw.println(name + ": " + vals[i] + "<br>");
        }
    }
}
out.println("</p></body></html>");

答案 1 :(得分:0)

你的

if ( vals != null) { //has no opening bracket

您需要将{添加到您的代码中。或者您需要在

之前删除}
out.println("</p></body></html>");