将数据从DB存储到Java类并使用JSP访问

时间:2018-08-11 19:37:38

标签: java jsp servlets model-view-controller

我在尝试使用模型java类访问数据库中存储的数据时遇到问题,我尝试使用该类在jsp页面上检索数据,但编译器给出了一个错误消息:java.lang.NullPointerException

我要使用Java类,数据库控制器和jsp页面在jsp页面上显示所有视频。

错误行:

exception

org.apache.jasper.JasperException:在第159行处理JSP页面/doctorallvideo.jsp时发生了异常

                 </tr>
                 </thead>
                 <tbody>
    error line--->    <% for(int i=0; i<vidlist.size(); i++){
                      video vid = vidlist.get(i);
                   %>
                  <tr>

Stacktrace:

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因:

java.lang.NullPointerException
org.apache.jsp.doctorallvideo_jsp._jspService(doctorallvideo_jsp.java:271)
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:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的模型课:

public class video {

private int videoId;
private String videoName;
private String description;
private String category;
private String fileName;
private String filepath;

public video(int videoId, String videoName, String description,String category, String fileName, String filepath) {
    super();

    this.videoId = videoId;
    this.videoName = videoName;
    this.description = description;
    this.category = category;
    this.fileName = fileName;
    this.filepath = filepath;

}


public video(String videoName, String description,String category, String fileName, String filepath) {

    this.videoName = videoName;
    this.description = description;
    this.category = category;
    this.fileName = fileName;
    this.filepath = filepath;   

}


public video() {
}



public String getCategory() {
    return category;
}


public void setCategory(String category) {
    this.category = category;
}


public String getFileName() {
    return fileName;
}


public void setFileName(String fileName) {
    this.fileName = fileName;
}


public String getFilepath() {
    return filepath;
}


public void setFilepath(String filepath) {
    this.filepath = filepath;
}


public int getVideoId() {
    return videoId;
}


public void setVideoId(int videoId) {
    this.videoId = videoId;
}


public String getVideoName() {
    return videoName;
}


public void setVideoName(String videoName) {
    this.videoName = videoName;
}


public String getDescription() {
    return description;
}


public void setDescription(String description) {
    this.description = description;
}

}

这是数据库控制器:

public ArrayList<video> getVideos() {
    try {
        // get person from database
        PreparedStatement ps = connection.prepareStatement("select * from video");
        ResultSet rs = ps.executeQuery();
        ArrayList<video> vidlist = new ArrayList<>();
        while (rs.next()) {
            // get doctor from database
            video vid = new video(rs.getInt("videoId"), rs.getString("videoName"), rs.getString("description"),rs.getString("category"), rs.getString("fileName")
                    ,rs.getString("filepath"));
            vidlist.add(vid);
        }
        return vidlist;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;

}

servlet文件:

public doctorallvideo() {
    super();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("Served at: ").append(request.getContextPath());


    try {
        int personId = (int) session.getAttribute("UserID");
        // get doctor details
        dbcontroller dbcon = new dbcontroller();
        ArrayList<video> vids = dbcon.getVideos();

        // add user id in the future 
        if (vids == null) {

        }
        RequestDispatcher rs = request.getRequestDispatcher("doctorallvideo.jsp");
        request.setAttribute("video", vids);
        rs.forward(request, response);
        return;

    } catch (Exception e) {
        // redirect to login
         redirectToVideoUpload(request, response);
    }

}

最后,这是我在jsp页面上添加的:

<%@ page
import="multimedia.video, java.util.ArrayList"%>   
<%  
        ArrayList<video> vidlist = (ArrayList<video>)request.getAttribute("video");
%>

<div class="col-sm-9 col-sm-offset-3 main">enter code here
            <h3 class="page-header">All Videos</h3>

,然后在jsp上显示:

<div class="col-sm-9 col-sm-offset-3 main" id="patients" style="display: none;">
            <table class="table table-striped table-hover">
                <thead>
                  <tr>
                    <th>Id</th>
                    <th>username</th>

                    <th>Blood Group</th>
                  </tr>
                 </thead>
                 <tbody>
                  <% for(int i=0; i<vidlist.size(); i++){
                      video vid = vidlist.get(i);
                      %>
                  <tr>
                    <td><%= vid.getVideoId() %></td>
                    <td><%= vid.getVideoName() %></td>
                  </tr>
                  <%} %>
                 </tbody>
            </table>
            </div>

1 个答案:

答案 0 :(得分:0)

我看到错误存在,因为您正在尝试访问未在代码片段中指定的 vidlist ,我的意思是在代码的这一部分<%代码片段%>

为什么不尝试在您的代码中进行检查。

            <tbody>
              <% 
              ArrayList<video> vidlist = (ArrayList<video>)request.getAttribute("video");
              for(int i=0; i<vidlist.size(); i++){
                  video vid = vidlist.get(i);
                  %>
              <tr>
                <td><%= vid.getVideoId() %></td>
                <td><%= vid.getVideoName() %></td>
              </tr>
              <%} %>
            </tbody>