jsp同html分开文件,在jsp中将Java和HTML分开?

本文探讨了如何在Servlet中将Java代码与HTML代码进行有效分离的方法。通过示例展示了如何使用JSP来展示mySql数据库中的数据,并讨论了最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我想将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;

%>

home idData

.....

try{

connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);

statement=connection.createStatement();

String sql ="SELECT * FROM table";

resultSet = statement.executeQuery(sql);

while(resultSet.next()){

%>

...

}

} catch (Exception e) {

e.printStackTrace();

}

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值