在jsp中将Java和HTML分开?

我想将Java代码(在servlet中)与html代码分开。

此代码显示了jsp中mySql表的数据。


不使用脚本的最佳实践是什么?


谢谢你的帮助。


<%

   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>


慕的地8271018
浏览 280回答 3
3回答

当年话下

使用Get方法和servlet。您可以在servlet中编写所有Java代码,如下所示:public void doGet (HttpServletRequest request, HttpServletResponse response)&nbsp;&nbsp; throws IOException, ServletException {&nbsp; &nbsp; String driverName = "com.mysql.jdbc.Driver";&nbsp; &nbsp; String connectionUrl = "jdbc:mysql://localhost/";&nbsp; &nbsp; String dbName = "db";&nbsp; &nbsp; String userId = "root";&nbsp; &nbsp; String password = "root";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp;Class.forName(driverName);&nbsp; &nbsp; &nbsp;} catch (ClassNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; Connection connection = null;&nbsp; &nbsp; Statement statement = null;&nbsp; &nbsp; ResultSet resultSet = null;&nbsp;&nbsp; &nbsp; try{&nbsp;&nbsp; &nbsp; connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);&nbsp; &nbsp; statement=connection.createStatement();&nbsp; &nbsp; String sql ="SELECT * FROM table";&nbsp; &nbsp; resultSet = statement.executeQuery(sql);&nbsp; &nbsp; // Now convert this result set into a class having field id and data..&nbsp; &nbsp; // like MyClass{ String id; String data;}&nbsp; &nbsp; // make a list of List<MyClass>list = new ArrayList<>();&nbsp; &nbsp; request.setAttribute("list",list);&nbsp;&nbsp; &nbsp; this.getServletContext().getRequestDispatcher("/jsp/yourPageName.jsp").&nbsp; &nbsp;include(request, response);&nbsp;}现在,使用在您的jsp文件中获取名为“ list”的此属性request.getAttribute("list");。将其输入列表。并对其进行迭代并进行相应的打印。

Smart猫小萌

即使没有Scriptlet或JSTL,也无法循环While,即使您可以将数据库获取作业放入servlet中也是如此。如果要删除任何服务器端脚本,则需要将体系结构分为两层。服务器端:servlet或JSP获取数据库并生成JSON或CSV客户端:html用AJAX调用服务器端以获取加载时的纯数据,然后使用javascript循环并且您不使用数据库连接池,而是直接建立数据库连接。挺贵的&nbsp;如果忘记关闭连接,则可能会耗尽资源。非常危险。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java