从数据库表中检索数据并将其显示在带有html的表中

时间:2014-04-17 19:04:42

标签: java jsp servlets jdbc scriptlet

我是一名初学者,目前正在研究我的最终学校项目,该项目需要从数据库表中检索数据并在html页面中显示数据。我搜索了很多但仍没有进展。我通过谷歌链接到这个网站,我看到一种格式,我尝试做同样的事情,但运行代码后我得到这个错误

例外情况报告

message An exception occurred processing JSP page /crimeinfo.jsp at line 32 description The server encountered an internal error that prevented it from fulfilling this request.

例外

org.apache.jasper.JasperException: An exception occurred processing JSP page /crimeinfo.jsp at line 32
29:     
30:     
31:     <%List <String> data =(List)request.getAttribute("data");
32:     Iterator <String> itr = data.iterator() ;
33:     for (itr = data.iterator();
34:     itr.hasNext();)
35:     {%>

我需要帮助。我不知道还能做什么。 thnks ...

viewcrime.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--web.xml code-->
<servlet>
<servlet-name>viewcrimereport</servlet-name>
<servlet-class>viewcrimereport</servlet-class>
<servlet-mapping>
<servlet-name>viewcrimereport</servlet-name>
<url-pattern>viewcrimereport</url-pattern>
</servlet-mapping></servlet>

viewcrimereport.java

public class viewcrimereport extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
     private ServletConfig config;
     String page = "crimeinfo.jsp";
     public void init(ServletConfig config)
         throws ServletException{
         this.config = config; }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out=response.getWriter();
    response.setContentType("text/html");
    Connection conn = null;
    String url = "jdbc:mysql://localhost/citycrime";
    String userName = "root";
    String passw = "jids";
    PreparedStatement pst = null;
    ResultSet rs;
    ArrayList<String> datalist = new ArrayList<String>();
    try{    
            int i=0;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection(url, userName, passw);
        pst = conn.prepareStatement("");
    String sql ="select suspectname,suspectaliases,suspectht,suspectgender,features,crimeaddress,crimetype,caseno,casestatus from crimereport";
        pst.executeQuery(sql);
        rs = pst.getResultSet();
        while(rs.next()){
            datalist.add(rs.getString("suspectname"));
            datalist.add(rs.getString("suspectaliases"));
            datalist.add(rs.getString("suspectht"));
            datalist.add(rs.getString("suspectgender"));
            datalist.add(rs.getString("features"));
            datalist.add(rs.getString("crimeaddress"));
            datalist.add(rs.getString("crimetype"));
            datalist.add(rs.getString("caseno"));
            datalist.add(rs.getString("casestatus"));}
        rs.close();pst.close(); }
    catch(Exception e){System.out.println("Exception is;"+e);}
    request.setAttribute("data",datalist);
    RequestDispatcher dispatcher = request.getRequestDispatcher(page);
    if (dispatcher != null){
        dispatcher.forward(request, response);
    }
        }
}    

crimeinfo.jsp

<%@ page language="java" import="java.sql.*" import="java.util.*" 
contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4
/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Crime Report</title>
</head>
<body>
    <table border="1" with="600">
    <tr><td><tb><td>SuspectName</td></b>
    <td><tb>SuspectAlias</b></td>
    <td><tb>suspectaliases</tb></td>
    <td><tb>Height</tb></td>
    <td><tb>suspectht</tb></td>
    <td><tb>Gender</tb></td>
    <td><tb>suspectgender</tb></td>
    <td><tb>Features</tb></td>
    <td><tb>features</tb></td>
    <td><tb>Address</tb></td>
    <td><tb>crimeaddress</tb></td>
    <td><tb>CrimeType</tb></td>
    <td><tb>crimetype</tb></td>
    <td><tb>CaseNumber</tb></td>
    <td><tb>caseno</tb></td>
    <td><tb>Status</tb></td>
    <td><tb>casestatus</tb></td></tr>
    <% Iterator <String> itr;%>
    <%List data =(List)request.getAttribute("datalist");
    for (itr = data.iterator();
    itr.hasNext();){%><tr>
    <%String s =(String)itr.next(); %>
    <td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
        <tr><td><%=itr.next()%>></td>
        <tr><td><%=itr.next()%>></td>
<tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td>
    <tr><td><%=itr.next()%>></td></tr><%} %>
</table>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

  1. 在第32行,设为Iterator itr = data.iterator() ;我建议您使用for循环的while(itr.hasNext() instaed以获得更清晰的代码。此外,for循环中的初始化部分是冗余的。

  2. 将您的xml文件重命名为web.xml并将其设为\WEB-INF\web.xml

  3. 在web.xml中,
  4. ,url-pattern应为<url-pattern>/viewcrimereport</url-pattern>

  5. 对于您的servlet,因为您已经扩展了HttpServlet类,所以不需要实现Servlet接口。 HttpServlet类已经实现了该接口

  6. 您尚未关闭连接。使用conn.close()

  7. 在你的jsp中,
  8. 应该是<%List data =(List)request.getAttribute("data");,因为你设置了名为data的请求属性,而不是“datalist”

  9. 不鼓励使用scriptlet(那些<% %>个东西)。 see this

  10. 由于您没有从servlet向浏览器输出任何内容,因此可以省略以下代码PrintWriter out=response.getWriter(); response.setContentType("text/html");

  11. 在我个人看来,我建议使用service代替doGet()。服务方法本身会调用doGet方法。此外,它还可以调用其他meyhods,如doPost,doHead等,如果需要的话。

答案 1 :(得分:0)

您可以使用datalist.get(column index)的{​​{1}}方法。有关详细信息,请参阅: http://www.tutorialspoint.com/java/java_arraylist_class.htm