在jsp中将Java和HTML分开?

Separate Java and HTML in jsp?

我想将 java 代码(在 servlet 中)与 html 代码分开。
此代码显示 jsp 中 mySql table 的数据。

不使用 scriptlet 的最佳做法是什么?

感谢您的帮助。

<%
   String id = request.getParameter("userId");
   String driverName = "com.mysql.jdbc.Driver";
   String connectionUrl = "jdbc:mysql://localhost/";
   String dbName = "db";
   String userId = "root";
   String password = "root";

   try {
   Class.forName(driverName);
   } catch (ClassNotFoundException e) {
   e.printStackTrace();
   }

   Connection connection = null;
   Statement statement = null;
   ResultSet resultSet = null;
%>
<table>
   <td style="border:none"><a href="index.jsp" class="LinkButton">home</a> <br></td>
   <tr>
      <th>id</th>
      <th>Data</th>
      .....
   </tr>
   <%
      try{ 
      connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
      statement=connection.createStatement();
      String sql ="SELECT * FROM table";

      resultSet = statement.executeQuery(sql);
      while(resultSet.next()){
   %>
   <tr>
      <td><%=resultSet.getString("id") %></td>
      <td><%=resultSet.getString("Data") %></td>
      ...
   </tr>
   <% 
      }

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

如果您只想分离 java 代码,那么您可以使用 JSTL

<jsp:include page="somecode.jsp" />

您可以在此处找到更多相关信息

https://www.tutorialspoint.com/jsp/jstl_core_import_tag.htm

https://www.safaribooksonline.com/library/view/head-first-servlets/9780596516680/ch09s18.html

我认为最好将逻辑与视图完全分开,有许多框架可以帮助您实现这一点,例如 Spring MVC。如果想在不使用任何框架的情况下实现,可以按照下面的link

https://www.javatpoint.com/MVC-in-jsp

使用 Get 方法和 servlet。 您可以在 servlet 中编写所有 java 代码,如下所示:

public void doGet (HttpServletRequest request, HttpServletResponse response) 
  throws IOException, ServletException {
    String driverName = "com.mysql.jdbc.Driver";
    String connectionUrl = "jdbc:mysql://localhost/";
    String dbName = "db";
    String userId = "root";
    String password = "root";

    try {
       Class.forName(driverName);
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
    }

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null; 
    try{ 
    connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
    statement=connection.createStatement();
    String sql ="SELECT * FROM table";
    resultSet = statement.executeQuery(sql);
    // Now convert this result set into a class having field id and data..
    // like MyClass{ String id; String data;}
    // make a list of List<MyClass>list = new ArrayList<>();
    request.setAttribute("list",list); 
    this.getServletContext().getRequestDispatcher("/jsp/yourPageName.jsp").
   include(request, response);
 }

现在使用 request.getAttribute("list"); 在 jsp 文件中获取名为 "list" 的属性。将其类型转换为列表。 并对其进行迭代并相应地打印。

没有 scriptlet 或 JSTL 就不可能循环 While,即使您可以将数据库获取作业​​放在 servlet 中也是如此。

如果要删除任何服务器端脚本,需要将架构分为两层。

  1. 服务器端:servlet 或 JSP 获取数据库并生成 JSON 或 CSV
  2. 客户端:html 使用 AJAX 调用服务器端以在加载时获取纯数据,然后使用 javascript
  3. 循环

而且你没有使用数据库连接池,而是直接建立数据库连接。这是相当昂贵的。如果您忘记关闭连接,您可能 运行 资源不足。非常危险。