setattribute要通过getattribute在jsp中获取,但是获取nullpointer

时间:2015-03-23 02:43:37

标签: java jsp servlets

我曾经通过setattribute尝试从servlet设置值,它必须通过setter和getter方法在没有EL和JSTl的情况下通过getattribute迭代jsp但是我得到了这个错误。我在谷歌搜索的帮助,但我找不到。

java.lang.NullPointerException
    org.apache.jsp.Home_jsp._jspService(Home_jsp.java:138)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这里是serlvet代码

    package servlet2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TreeSet;

import javafx.css.PseudoClass;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.Dao;
import jdbc.jdbcconnection;

/**
 * Servlet implementation class Homepage
 */
//@WebServlet(asyncSupported = true, urlPatterns = { "/Homepage" })
public class Homepage extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Homepage() {
        super();
        // TODO Auto-generated constructor stub
    }




    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        // TODO Auto-generated method stub

        System.out.println("inside==========>");

        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String phone=request.getParameter("Phone");
        String Deptmart=request.getParameter("Dep");
        String gender=request.getParameter("gender");
        String country=request.getParameter("country");

        Dao d=new Dao();

        try
        {
        jdbcconnection jc=new jdbcconnection();

        Connection con=jc.getconnection();

        String sql="insert into homepage(name,password,phone,Dept,gender,Country) values(?,?,?,?,?,?)";

            PreparedStatement ps=con.prepareStatement(sql);

            ps.setString(1, name);
            ps.setString(2, password);
            ps.setString(3, phone);
            ps.setString(4, Deptmart);
            ps.setString(5, gender);
            ps.setString(6, country);

            System.out.println("ps=========>"+ps);

            ps.execute();

            con.commit();


            String select="select * from homepage";

            PreparedStatement ps1=con.prepareStatement(select);

            ResultSet rs=ps.executeQuery();

            ArrayList t=new ArrayList();
            while(rs.next())
            {
                d.setid(rs.getInt(1));
                d.setName(rs.getString(2));
                d.setPassword(rs.getString(3));
                d.setPhone(rs.getInt(4));
                d.setDeptmart(rs.getString(5));
                d.setGender(rs.getString(6));
                d.setCountry(rs.getString(7));

                t.add(d);
            }

            request.setAttribute("users",t);

            RequestDispatcher rs1=request.getRequestDispatcher("/Home.jsp");
            rs1.forward(request, response);
                }

        catch (SQLException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Jsp页面:

<%@page import="servlet.Dao,java.util.*" %>




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Homepage" method="get">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name" id="name"></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="text" name="password" id="password"></td>
        <tr>
            <td>Phone:</td>
            <td><input type="text" name="Phone" id="Phone">
        </tr>
        </td>
        <tr>
            <td>Department:</td>
            <td><select id="Dep" name="Dep">
                    <option value="it">IT</option>
                    <option value="finace">Finace</option>
                    <option value="market">Marketing</option>
            </select></td>
        </tr>
        <tr>
            <td>Gender:</td>
            <td><input type="radio" name="gender" id="gender" Value="Male">Male
                <input type="radio" name="gender" id="gender" Value="Female">Female</td>
        </tr>


        <tr>
            <td>Country:</td>
            <td><input type="checkbox" name="country" id="country"
                Value="India">India <input type="checkbox" name="country"
                id="country" Value="Other">Other</td>
        </tr>

        <br>
        <tr>
            <td><input type="submit" name="Add" Value="ADD" ></td>
            <td><input type="button" name="Clear" Value="CLEAR"></td>
        </tr>
    </table>
</form>

<table border="1">

<tr><td>Id</td><td>name</td>
<td>Password</td>
<td>Phone</td>
<td>Deptmart</td>
<td>Gender</td>
<td>country</td>
<td>Edit</td>
<td>Delete</td>
</tr>

 <%
 try
 {
        List<Dao> al1 = (List) request.getAttribute("users");
      //  System.out.println(al1); // prints null
        for(Dao user : al1) {



        %>

<tr>
<td><%=user.getid() %></td>

<td><%=user.getName() %></td>
<td><%=user.getPassword() %></td>
<td><%=user.getPhone() %></td>
<td><%=user.getDeptmart() %></td>
<td><%=user.getGender() %></td>
<td><%=user.getCountry() %></td>
<td></td>
<td></td>
</tr>
<%} }

 catch(Exception e)
 {
    e.printStackTrace();  
 }
%>




</table>


</body>
</html>

的web.xml

<servlet>
    <servlet-name>Homepage</servlet-name>
    <servlet-class>servlet2.Homepage</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>Homepage</servlet-name>
    <url-pattern>/Homepage</url-pattern>
  </servlet-mapping>

0 个答案:

没有答案