1

我只是使用按钮将控制从 jsp 传递给 Servlet。但是NullPointerException当我单击提交按钮时,我在评论的行中进入 servlet。请告诉我错误。
提前谢谢。

这是我的 Jsp 代码

<form action="JQGridServlet">
    <input type="submit" name="submit" value="submit"/>
</form>

这是我的 Servlet 代码

 public class JQGridServlet extends HttpServlet 
 {
  protected void processRequest(HttpServletRequest request, HttpServletResponse               

 response) throws Exception {        
    PrintWriter out = response.getWriter();
    IPConverter conv=new IPConverter();


    System.setProperty("log4j.defaultInitOverride","true");
    LogManager.resetConfiguration();
    LogManager.getRootLogger().addAppender(new NullAppender());

      try {

   //Null  Exception at this line            

        if (request.getParameter("action").equals("fetchData"))     ///////Exception
        {
            response.setContentType("text/xml;charset=UTF-8");                


            Object o = request.getAttribute("page");
            System.out.println("----------------------o="+o);
            String status = request.getParameter("status");

            String rows = request.getParameter("rows");
            String page = request.getParameter("page");
            if(request.getParameter("submit")!=null)
            {
            System.out.println(request.getParameter("submit"));
            }
            //  String KeyName=request.getParameter("KeyName");
            //System.out.println(KeyName);
            int totalPages = 0;
            int totalCount = 58000;

            if (totalCount > 0) {
                if (totalCount % Integer.parseInt(rows) == 0) {
                    totalPages = totalCount / Integer.parseInt(rows);
                } else {
                    totalPages = (totalCount + Integer.parseInt(rows) - 1) /     

              Integer.parseInt(rows);
                }

            } else {
                totalPages = 0;
            }

            out.print("<?xml version='1.0' encoding='utf-8'?>\n");
            out.print("<rows>");
            out.print("<page>" + request.getParameter("page") + "</page>");

            out.print("<total>" + totalPages + "</total>");
            out.print("<records>" + 58000 + "</records>");

            EventQuery eq=new EventQuery();
            int firstRecord = (Integer.parseInt(page) - 1)  
    *Integer.parseInt(rows);
            int lastRecord = Math.min(firstRecord + Integer.parseInt(rows), totalCount);
            List<ColumnSlice<String, Long>> row=eq.query(Integer.parseInt(request.getParameter("page")),Integer.parseInt(rows));

            for (int i=firstRecord,j=0;i<lastRecord;i++,j++) {

                out.print("<row id='" + i + "'>");
                out.print("<cell>" + (i+1) + "</cell>");
                out.print("<cell>" + row.get(j).getColumnByName("USERIDEN").getValue() +"</cell>");
                 out.print("<cell>" + conv.convert(row.get(j).getColumnByName("SOURCEIP").getValue()) +"</cell>");

                out.print("<cell>" + row.get(j).getColumnByName("BYTESENT").getValue()+"</cell>");
                out.print("<cell>" + row.get(j).getColumnByName("BYTERECV").getValue()+"</cell>");
                //out.print("<cell><![CDATA[<a href='ViewStd.jsp'>View</a>]]></cell>");
                out.print("</row>");
                //srNo++;                
            }
            out.print("</rows>");
        }

    } 
      catch(Exception e)
      {
          e.printStackTrace();  
      }
      finally {
        out.close();
    }
} 


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    try {
        processRequest(request, response);                 ///////Exception
       } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    try {
        processRequest(request, response);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


@Override
public String getServletInfo() {
    return "Short description";
}

 }
4

2 回答 2

2

发生异常是request.getParameter("action")因为null. action它为空,因为您的表单中没有命名字段。

如果您将表单修改为:

<form action="JQGridServlet">
 <input name="action"></input>
 <input type="submit" name="submit" value="submit"/>
</form>

当然,命名的表单元素的类型action取决于您的逻辑。

于 2013-03-20T13:29:48.983 回答
0

if在您的语句中首先添加空检查:

 if (request.getParameter("action") != null && request.getParameter("action").equals("fetchData")) {

 }
于 2013-03-20T13:29:40.720 回答