jstljsp include servlet使jsp为空

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

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

  1. <%@page import="SW.models.StudentOrg"%>
  2. <%@page contentType="text/html" pageEncoding="utf-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. <jsp:include page="GetOrgServlet" flush="true" />
  5. <c:import url="/includes/header.html" />
  6. <body>
  7. <div id='container'>
  8. <c:import url="/includes/navigation.html" />
  9. <aside>
  10. <img src='https://projecthelping.org/wp-content/uploads/2017/11/Your-Logo-here.png' width="200" height="200" alt='Organization Logo'>
  11. <h3>[List of Upcoming Events Here]</h3>
  12. <p>[Contact Information Here]</p>
  13. </aside>
  14. <section id='main'>
  15. <h2>[Student Organization Name Here]</h2>
  16. <p>
  17. [Student Organization Description Here]
  18. </p>
  19. <p>
  20. [Student Organizations Q&A Section Here]
  21. </p>
  22. <table>
  23. <th>Org ID</th>
  24. <th>Org Name</th>
  25. <th>Org Description</th>
  26. <c:forEach var="studentOrg" items="${studentOrgList.studentOrg}">
  27. <tr>
  28. <td>${studentOrg.OrgID}</td>
  29. <td>${studentOrg.OrgName}</td>
  30. <td>${studentOrg.OrgDescription}</td>
  31. </tr>
  32. </c:forEach>
  33. </table>
  34. </section>
  35. <c:import url="/includes/footer.jsp" />
  36. </div>
  37. </body>
  38. </html>

有问题的servlet:

  1. package SW.controllers;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import SW.data.DB;
  9. import SW.models.StudentOrg;
  10. import SW.models.StudentOrgList;
  11. public class GetOrgServlet extends HttpServlet {
  12. /**
  13. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  14. * methods.
  15. *
  16. * @param request servlet request
  17. * @param response servlet response
  18. * @throws ServletException if a servlet-specific error occurs
  19. * @throws IOException if an I/O error occurs
  20. */
  21. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  22. throws ServletException, IOException {
  23. HttpSession session = request.getSession();
  24. StudentOrgList studentOrgList = DB.getStudentOrgs();
  25. session.setAttribute("studentOrgList", studentOrgList);
  26. }
  27. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  28. /**
  29. * Handles the HTTP <code>GET</code> method.
  30. *
  31. * @param request servlet request
  32. * @param response servlet response
  33. * @throws ServletException if a servlet-specific error occurs
  34. * @throws IOException if an I/O error occurs
  35. */
  36. @Override
  37. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  38. throws ServletException, IOException {
  39. processRequest(request, response);
  40. }
  41. /**
  42. * Handles the HTTP <code>POST</code> method.
  43. *
  44. * @param request servlet request
  45. * @param response servlet response
  46. * @throws ServletException if a servlet-specific error occurs
  47. * @throws IOException if an I/O error occurs
  48. */
  49. @Override
  50. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  51. throws ServletException, IOException {
  52. processRequest(request, response);
  53. }
  54. /**
  55. * Returns a short description of the servlet.
  56. *
  57. * @return a String containing servlet description
  58. */
  59. @Override
  60. public String getServletInfo() {
  61. return "Short description";
  62. }// </editor-fold>
  63. }

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

  1. package SW.data;
  2. import java.sql.*;
  3. import SW.models.*;
  4. public class DB {
  5. public static StudentOrg selectStudentOrg(Integer OrgID) {
  6. ConnectionPool pool = ConnectionPool.getInstance();
  7. Connection connection = pool.getConnection();
  8. PreparedStatement ps = null;
  9. ResultSet rs = null;
  10. String query = "SELECT * FROM org "
  11. + "WHERE OrgID = ?";
  12. try {
  13. ps = connection.prepareStatement(query);
  14. ps.setInt(1, OrgID);
  15. rs = ps.executeQuery();
  16. StudentOrg studentOrg = null;
  17. if (rs.next()) {
  18. studentOrg = new StudentOrg();
  19. studentOrg.setOrgID(rs.getInt("orgID"));
  20. studentOrg.setOrgName(rs.getString("orgName"));
  21. studentOrg.setOrgDescription(rs.getString("orgDescription"));
  22. }
  23. return studentOrg;
  24. } catch (SQLException e) {
  25. System.out.println(e);
  26. return null;
  27. } finally {
  28. DBUtil.closeResultSet(rs);
  29. DBUtil.closePreparedStatement(ps);
  30. pool.freeConnection(connection);
  31. }
  32. }
  33. public static StudentOrgList getStudentOrgs() {
  34. ConnectionPool pool = ConnectionPool.getInstance();
  35. Connection connection = pool.getConnection();
  36. PreparedStatement ps = null;
  37. ResultSet rs = null;
  38. String query = "SELECT * FROM org";
  39. try {
  40. ps = connection.prepareStatement(query);
  41. rs = ps.executeQuery();
  42. StudentOrgList studentOrgList = new StudentOrgList();
  43. while (rs.next()) {
  44. StudentOrg studentOrg = new StudentOrg();
  45. studentOrg.setOrgID(rs.getInt("OrgID"));
  46. studentOrg.setOrgName(rs.getString("OrgName"));
  47. studentOrg.setOrgDescription(rs.getString("OrgDescription"));
  48. studentOrgList.addStudentOrg(studentOrg);
  49. }
  50. return studentOrgList;
  51. } catch (SQLException e) {
  52. System.err.println(e);
  53. return null;
  54. } finally {
  55. DBUtil.closeResultSet(rs);
  56. DBUtil.closePreparedStatement(ps);
  57. pool.freeConnection(connection);
  58. }
  59. }
  60. }

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

  1. package SW.data;
  2. import java.sql.*;
  3. import javax.sql.DataSource;
  4. import javax.naming.InitialContext;
  5. import javax.naming.NamingException;
  6. public class ConnectionPool {
  7. private static ConnectionPool pool = null;
  8. private static DataSource dataSource = null;
  9. private ConnectionPool() {
  10. try {
  11. InitialContext ic = new InitialContext();
  12. dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/milestone3");
  13. } catch (NamingException e) {
  14. System.out.println(e);
  15. }
  16. }
  17. public static synchronized ConnectionPool getInstance() {
  18. if (pool == null) {
  19. pool = new ConnectionPool();
  20. }
  21. return pool;
  22. }
  23. public Connection getConnection() {
  24. try {
  25. return dataSource.getConnection();
  26. } catch (SQLException e) {
  27. System.out.println(e);
  28. return null;
  29. }
  30. }
  31. public void freeConnection(Connection c) {
  32. try {
  33. c.close();
  34. } catch (SQLException e) {
  35. System.out.println(e);
  36. }
  37. }
  38. }

数据库工具

  1. package SW.data;
  2. import java.sql.*;
  3. public class DBUtil {
  4. public static void closeStatement(Statement s) {
  5. try {
  6. if (s != null) {
  7. s.close();
  8. }
  9. } catch (SQLException e) {
  10. System.out.println(e);
  11. }
  12. }
  13. public static void closePreparedStatement(Statement ps) {
  14. try {
  15. if (ps != null) {
  16. ps.close();
  17. }
  18. } catch (SQLException e) {
  19. System.out.println(e);
  20. }
  21. }
  22. public static void closeResultSet(ResultSet rs) {
  23. try {
  24. if (rs != null) {
  25. rs.close();
  26. }
  27. } catch (SQLException e) {
  28. System.out.println(e);
  29. }
  30. }
  31. }

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

暂无答案!

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

相关问题