我是JSP Servlet的新手,我想要做的任务就是在JSP页面中使用servlet的ResultSet数据...当我运行代码时它会给我一些错误,比如
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
PWC6199: Generated servlet error:
source value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
target value 1.5 is obsolete and will be removed in a future release
PWC6199: Generated servlet error:
To suppress warnings about obsolete options, use -Xlint:-options.
PWC6197: An error occurred at line: 148 in the jsp file: /products.jsp
PWC6199: Generated servlet error:
cannot find symbol
symbol: class ResultSet
location: class org.apache.jsp.products_jsp
PWC6197: An error occurred at line: 148 in the jsp file: /products.jsp
PWC6199: Generated servlet error:
method processRequest in class jdbc.units cannot be applied to given types;
required: javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse
found: no arguments
reason: actual and formal argument lists differ in length
这是我的代码..
units.java
package jdbc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.json.*;
import javax.servlet.http.HttpSession;
public class units extends HttpServlet {
protected ResultSet processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ResultSet rs = null;
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(units.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gst_application", "root", "");
String sql = "SELECT * FROM `product_units` ORDER BY `product_unit_id` ASC";
PreparedStatement ps = null;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
} catch (SQLException ex) {
Logger.getLogger(units.class.getName()).log(Level.SEVERE, null, ex);
out.print(ex);
}
}
return rs;
}
}
product.jsp
<%@page import="jdbc.units"%>
<select class="form-control" name="product_unit" required="required">
<%
units obj = new units();
ResultSet rs = obj.processRequest();
while(rs.next())
{
out.print("<option>"+rs.getString("unit_val")+"</option>");
}
%>
</select>
答案 0 :(得分:1)
首先警告:您不应直接在页面中使用ResultSet。您应该将结果放在一个中间对象中,然后关闭ResultSet,Statement以及必要时的连接。
话虽如此,据我所知,您在JSP页面中缺少import语句,因此JSP编译器无法找到ResultSet类。尝试在JSP页面中导入jdbc包。
答案 1 :(得分:0)
您可以将ResultSet
对象放在List中并将其传递给您的页面,并在JSP中提取此列表。在这里,我假设Demo
是您的班级名称。您可以根据您的班级名称更改代码。不要忘记关闭连接。
在Servlet中
Demo d = null;
List <Demo> myList = new ArrayList<>();
while (rs.next()) {
myList.add(d);
}
rs.close();
ps.close();
con.close();
request.setAttribute("List", myList);
将您的响应转发给JSP文件,如下所示:
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/pageName.jsp");
requestDispatcher.forward(request, response);
在JSP中
使用此标记:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
使用forEach
<c:forEach items="${List}" var="theList">
<tr>
<td></td>
<td>${theList.attributeName}</td>
...
</tr>
</c:forEach>