在jsp中分离Java和HTML?
我想将java代码(在servlet中)与html代码分开。在jsp中分离Java和HTML?,java,html,jsp,servlets,servlet-3.0,Java,Html,Jsp,Servlets,Servlet 3.0,我想将java代码(在servlet中)与html代码分开。 此代码在jsp中显示mySql表的数据 不使用scriptlet的最佳实践是什么 谢谢你的帮助 <% String id = request.getParameter("userId"); String driverName = "com.mysql.jdbc.Driver"; String connectionUrl = "jdbc:mysql://localhost/"; String dbName
此代码在jsp中显示mySql表的数据 不使用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" />
你可以在这里找到更多关于它的信息
我认为最好将您的逻辑与视图完全分离,有许多框架可以帮助您实现这一点,比如SpringMVC。如果您想在不使用任何框架的情况下实现,可以按照下面的链接进行操作
使用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);
}
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException、ServletException{
String driverName=“com.mysql.jdbc.Driver”;
字符串connectionUrl=“jdbc:mysql://localhost/";
字符串dbName=“db”;
字符串userId=“root”;
字符串password=“root”;
试一试{
类forName(driverName);
}catch(classnotfounde异常){
e、 printStackTrace();
}
连接=空;
Statement=null;
ResultSet ResultSet=null;
试试{
connection=DriverManager.getConnection(connectionUrl+dbName、userId、密码);
statement=connection.createStatement();
String sql=“从表中选择*”;
resultSet=statement.executeQuery(sql);
//现在将此结果集转换为具有字段id和数据的类。。
//像MyClass{String id;String data;}
//创建Listlist=new ArrayList()的列表;
setAttribute(“列表”,列表);
这个.getServletContext().getRequestDispatcher(“/jsp/yourPageName.jsp”)。
包括(请求、响应);
}
现在,使用request.getAttribute(“list”);
在jsp文件中获取名为“list”的属性。将其键入列表。
然后迭代它并相应地打印。如果没有scriptlet或JSTL,就不可能循环While,即使您可以将数据库获取作业放在servlet中 如果要删除任何服务器端脚本,需要将体系结构分为两层