首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将数据库中的数据存储到java类中并使用JSP进行访问

将数据库中的数据存储到java类中并使用JSP进行访问
EN

Stack Overflow用户
提问于 2018-08-12 03:37:38
回答 1查看 50关注 0票数 0

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

我想要做的是使用java类、db控制器和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:
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;
}

}

这是db控制器:

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>
EN

回答 1

Stack Overflow用户

发布于 2018-08-12 07:20:41

我看到这个错误的存在是因为您试图访问视频列表,它没有在代码片段中指定我的意思是在代码的这一部分<% code fragment %>

为什么不在你的代码中尝试一下并检查一下。

            <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51803122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档