jstljsp include servlet使jsp为空

sg24os4d  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(192)

我正在编写一段代码来显示来自mysql数据库的组织列表。我使用的是jdbc领域表单身份验证,因此我知道我的数据库可以被查询并正常运行。当我排除servlet时,我的jsp页面会像普通页面一样显示所有内容,但是当我包含servlet时,我的页面会显示空白,只有css的背景色,没有其他内容。我非常感谢您能帮助我找出发生这种情况的原因和/或解决方法,以便我可以从数据库中显示此列表。所有相关代码如下:
有问题的jsp页面:

<%@page import="SW.models.StudentOrg"%>
<%@page contentType="text/html" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="GetOrgServlet" flush="true" />
<c:import url="/includes/header.html" />
<body>
    <div id='container'>

        <c:import url="/includes/navigation.html" />

        <aside>
            <img src='https://projecthelping.org/wp-content/uploads/2017/11/Your-Logo-here.png' width="200" height="200"   alt='Organization Logo'>
            <h3>[List of Upcoming Events Here]</h3>
            <p>[Contact Information Here]</p>
        </aside>

        <section id='main'>

            <h2>[Student Organization Name Here]</h2>   
            <p>
                [Student Organization Description Here] 
            </p>

            <p>
                [Student Organizations Q&A Section Here]
            </p>

            <table>
                <th>Org ID</th>
                <th>Org Name</th>
                <th>Org Description</th>
                    <c:forEach var="studentOrg" items="${studentOrgList.studentOrg}"> 
                    <tr>
                        <td>${studentOrg.OrgID}</td>
                        <td>${studentOrg.OrgName}</td>
                        <td>${studentOrg.OrgDescription}</td>
                    </tr>
                </c:forEach>
            </table>

        </section>

        <c:import url="/includes/footer.jsp" />

    </div>
</body>
</html>

有问题的servlet:

package SW.controllers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import SW.data.DB;
import SW.models.StudentOrg;
import SW.models.StudentOrgList;

public class GetOrgServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();
        StudentOrgList studentOrgList = DB.getStudentOrgs();
        session.setAttribute("studentOrgList", studentOrgList);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

java(在存储查询的地方,我在servlet中使用getstudentorgs)

package SW.data;

import java.sql.*;
import SW.models.*;

public class DB {

    public static StudentOrg selectStudentOrg(Integer OrgID) {
        ConnectionPool pool = ConnectionPool.getInstance();
        Connection connection = pool.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        String query = "SELECT * FROM org "
                + "WHERE OrgID = ?";
        try {
            ps = connection.prepareStatement(query);
            ps.setInt(1, OrgID);
            rs = ps.executeQuery();
            StudentOrg studentOrg = null;
            if (rs.next()) {
                studentOrg = new StudentOrg();
                studentOrg.setOrgID(rs.getInt("orgID"));
                studentOrg.setOrgName(rs.getString("orgName"));
                studentOrg.setOrgDescription(rs.getString("orgDescription"));
            }
            return studentOrg;
        } catch (SQLException e) {
            System.out.println(e);
            return null;
        } finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            pool.freeConnection(connection);
        }
    }

    public static StudentOrgList getStudentOrgs() {
        ConnectionPool pool = ConnectionPool.getInstance();
        Connection connection = pool.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        String query = "SELECT * FROM org";
        try {
            ps = connection.prepareStatement(query);
            rs = ps.executeQuery();
            StudentOrgList studentOrgList = new StudentOrgList();
            while (rs.next()) {
                StudentOrg studentOrg = new StudentOrg();
                studentOrg.setOrgID(rs.getInt("OrgID"));
                studentOrg.setOrgName(rs.getString("OrgName"));
                studentOrg.setOrgDescription(rs.getString("OrgDescription"));
                studentOrgList.addStudentOrg(studentOrg);
            }
            return studentOrgList;
        } catch (SQLException e) {
            System.err.println(e);
            return null;
        } finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            pool.freeConnection(connection);
        }
    }
}

正在查询的表的图片
连接池

package SW.data;

import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ConnectionPool {

    private static ConnectionPool pool = null;
    private static DataSource dataSource = null;

    private ConnectionPool() {
        try {
            InitialContext ic = new InitialContext();
            dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/milestone3");
        } catch (NamingException e) {
            System.out.println(e);
        }
    }

    public static synchronized ConnectionPool getInstance() {
        if (pool == null) {
            pool = new ConnectionPool();
        }
        return pool;
    }

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            System.out.println(e);
            return null;
        }
    }

    public void freeConnection(Connection c) {
        try {
            c.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}

数据库工具

package SW.data;

import java.sql.*;

public class DBUtil {

    public static void closeStatement(Statement s) {
        try {
            if (s != null) {
                s.close();
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }

    public static void closePreparedStatement(Statement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}

再说一次,我不一定要这样显示列表,这只是一种方法,我以为我知道怎么做,因为我以前这么做了,但似乎有些不对劲,我找不到它的任何地方。因此,我很感激任何关于这个解决方案或不同的建议!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题