qq_35265058
qq_35265058
采纳率0%
2017-03-25 09:26

httpservletrequest中的request添加一个实例的类

重写了doPost方法,其中Student是一个学生信息类(包含set,get方法),studentDAO是一个sql处理语句方法类。我想把得到的结果Student类直接传出去,再用get方法调用,而不是先取出Student中的数据再传递数据。
所以我直接把得到的Student作为属性值添加进了request。

 request.setCharacterEncoding("utf-8");
        int id=Integer.parseInt(request.getParameter("idd"));
        String name=request.getParameter("name");
        String sex=request.getParameter("sex");
        int age=Integer.parseInt(request.getParameter("age"));
        float weight=Float.parseFloat(request.getParameter("weight"));
        float hight=Float.parseFloat(request.getParameter("hight"));
        Student stu=new Student(id,name,sex,age,weight,hight);
        studentDAO s=new studentDAO();
        Student stu2=null;
        try{stu2=s.find(stu);
        }catch(Exception e){
            e.printStackTrace();
        }
        request.setAttribute("student",stu2);
        request.getRequestDispatcher("/test.jsp").forward(request, response);

然后在test.jsp中取出Student类:

  <%request.setCharacterEncoding("utf-8");
  Student stu=null;
  try{
   stu=(Student)request.getAttribute("student");
   if(stu!=null){out.print("1");}
  }catch(Exception e){
  e.printStackTrace();}
   %>
 **   <%=stu.toString() %>**
   <table border="0" width="238" height="252">
  <tr><td>学号</td><td><%=stu.getId() %></td></tr>
  <tr><td>姓名</td><td><%=stu.getName() %></td></tr>
  <tr><td>性别</td><td><%=stu.getSex() %></td></tr>
  <tr><td>年龄</td><td><%=stu.getAge() %></td></tr>
  <tr><td>体重</td><td><%=stu.getWeigth() %></td></tr>
  <tr><td>身高</td><td><%=stu.getHight() %></td></tr>
  </table>

结果运行的时候出现以下错误:

 org.apache.jasper.JasperException: An exception occurred processing JSP page /test.jsp at line 36(加粗的哪一行)

 Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
**  servlet.find.doPost(find.java:71)**
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
    org.apache.jsp.test_jsp._jspService(test_jsp.java:156)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    servlet.find.doPost(find.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.11 logs.

加粗的哪一行对应:

 request.getRequestDispatcher("/test.jsp").forward(request, response);

我看到有个空指针异常,是不是因为request不能保存实例类?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_37733891 qq_37733891 4年前

    用session传递就可以了

    点赞 评论 复制链接分享

相关推荐