0

I am creating a standard dynamic web project including a Servlet, a filter to filter the request and jsp pages. I am taking the number of books from the welcome.jsp and depending on the count I am populating the input boxes on the add.jsp. I want to filter the data coming through the add.jsp. When I verify data through Regex, its showing me error. Also is my flow correct i.e. from the start FILTER --> CONTROLLER --> FILTER --> PAGE and so on???

Error: java.lang.NumberFormatException: null

full trace is like:


HTTP Status 500 - java.lang.NumberFormatException: null

type Exception report

message java.lang.NumberFormatException: null

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NumberFormatException: null
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    com.filter.RequestFilter.doFilter(RequestFilter.java:107)

root cause

java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Unknown Source)
    java.lang.Integer.parseInt(Unknown Source)
    org.apache.jsp.WEB_002dINF.add_jsp._jspService(add_jsp.java:139)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    com.filter.RequestFilter.doFilter(RequestFilter.java:107)

The code where its showing is:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    // place your code here
    boolean flag = true;
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpServletRequest.getServletPath();
    System.out.println(httpServletRequest.getServletPath());


    String address = InetAddress.getLocalHost().getHostAddress();
    System.out.println(address); 


    // For the first time (Welcome page) i will be 0 so that it will perform first if

    if(i == 0)
    {
        System.out.println(i);
        if(verify(address))
        {
            chain.doFilter(request, response);
            i++;
        }

        else
        {
            RequestDispatcher requestDispatcher = httpServletRequest.getServletContext().getRequestDispatcher("/WEB-INF/forbidden.jsp");
            requestDispatcher.forward(request, response);
            return;
        }

    }

    // For the second time (Controller) i will be 1 so that it will perform first if


    else if(i == 1)
    {
        System.out.println(i);
        i++;
        System.out.println("after ++"+i);
        chain.doFilter(request, response);
    }

    // For the second time (add page) i will be 2 so that it will perform first if


    else if(i == 2)
    {

        if(specialChara(httpServletRequest))
            {
                System.out.println(i);
                System.out.println("done");
                i++;
                chain.doFilter(request, response);
            }

            else
            {
                System.out.println(i);
                flag = false;
                System.out.println(""+httpServletRequest.toString()+ "\n"+httpResponse.toString());
                RequestDispatcher requestDispatcher = httpServletRequest.getServletContext().getRequestDispatcher("/WEB-INF/add.jsp");
                requestDispatcher.forward(request, response);

            }
    }

    // For the third time (Controller) i will be 2 so that it will perform first if

    else if(i == 3)
    {
        System.out.println(i);
        i++;
        chain.doFilter(request, response);
        i = 0;
    }

Its giving error on the line of forward() in the loop where i==2. What might be the issue?

link of add.jsp is https://docs.google.com/document/d/1fOzymYvlLXS577DrSrznRoeBnI7_hMmuqzFoK02xKoU/edit?usp=sharing

4

1 回答 1

1

From the stack trace it seems that the problem is not with your java class, but rather your JSP. My guess is in the line

int no = Integer.parseInt(request.getParameter("no"));

no seems to be null.Also you are better off using and other conditional tags rather than using scriptlets in your JSP.

于 2013-02-21T05:48:05.627 回答