问题描述
currentPage为什么会得不到,出现空指针异常?org.apache.jasper.JasperException:AnexceptionoccurredprocessingJSPpage/index.jspatline7774:</tr>75:</thead>76:<%77:intpageCount=(Integer)request.getAttribute("pageCount");78:intcurrentPage=(Integer)request.getAttribute("currentPage");79:80:%>Stacktrace:org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rootcausejava.lang.NullPointerExceptionorg.apache.jsp.index_jsp._jspService(index_jsp.java:135)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:803)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
解决方案
解决方案二:
jsp页面:<%intpageCount=(Integer)request.getAttribute("pageCount");intcurrentPage=(Integer)request.getAttribute("currentPage");%>servlet:publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html");PrintWriterout=response.getWriter();request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");intcurrentPage=1;//分析当前页码Stringpages=request.getParameter("currentPage");if(pages==null||pages.length()==0)pages="1";try{currentPage=Integer.parseInt(pages);}catch(Exceptione){currentPage=1;}intpageSize=3;//每页显示的数据数Studentstudent=newStudent();StudentBeansb=newStudentBean();//List<Student>studlist=newArrayList<Student>();List<Student>studlist=sb.findStudentByPage(currentPage,pageSize);intrecordCount=0;recordCount=sb.getStudentTotalCount();//记录总数intpageCount=0;if(recordCount%pageSize==0)pageCount=recordCount/pageSize;//计算总页数elsepageCount=recordCount/pageSize+1;request.setAttribute("currentPage",currentPage);request.setAttribute("pageCount",pageCount);request.setAttribute("studlist",studlist);getServletContext().getRequestDispatcher("/index.jsp").forward(request,response);}
解决方案三:
地址栏输入servlet和超级练级servlet都是doGet请求你这里代码看不出有什么问题你重写了doPost方法,那你的servlet中的doGet调用了doPost(request,response)了嘛??
解决方案四:
当然会报错,你在servlet中重定向了请求getServletContext().getRequestDispatcher("/index.jsp").forward(request,response);,request对象消亡了。我建议LZ好好理解4大作用域。