问题描述
您好我是新来HAVA,我有在JSP页面中一个ArrayList查看我的记录有问题,
每当我加载网页我得到:
[content.animalBean@1e8614a,content.animalBean@14b52aa,content.animalBean@2026f3,content.animalBean@dd20b6,content.animalBean@18eb00c] 1,它不是数据库记录
这里是我的code:
selectAnimalServlet:
Hello i'm new to hava and i'm having a problem viewing my records from an arraylist in JSP page,whenever i load the page i get:[content.animalBean@1e8614a, content.animalBean@14b52aa, content.animalBean@2026f3, content.animalBean@dd20b6, content.animalBean@18eb00c] 1 which is not the database recordshere is my code:selectAnimalServlet:
package content;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class selectAnimalServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try
{
List<animalBean> beans = DAO.selectListAnimal();
request.setAttribute("beans", beans);
request.getRequestDispatcher("checkAnimal.jsp").forward(request, response);
}
catch (Throwable theException)
{
System.out.println(theException);
}
}
}
AnimalBean:
AnimalBean:
package content;
public class animalBean {
private String animalName;
private String animalDob;
private String animalGender;
private String animalSource;
private String animalBreed;
private String animalRemark;
public String getAnimalName() {return animalName;}
public String getAnimalDob() {return animalDob;}
public String getAnimalGender() {return animalGender;}
public String getAnimalSource() {return animalSource;}
public String getAnimalBreed() {return animalBreed;}
public String getAnimalRemark() {return animalRemark;}
public void setAnimalName(String animalName) {this.animalName = animalName;}
public void setAnimalDob(String animalDob) {this.animalDob = animalDob;}
public void setAnimalGender(String animalGender) {this.animalGender = animalGender;}
public void setAnimalSource(String animalSource) {this.animalSource = animalSource;}
public void setAnimalBreed(String animalBreed) {this.animalBreed = animalBreed;}
public void setAnimalRemark(String animalRemark) {this.animalRemark = animalRemark;}
}
DAO类:
package content;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class DAO
{
static Connection currentCon = null;
static ResultSet rs = null;
public static loginAuth login(loginAuth bean) {
//preparing some objects for connection
Statement stmt = null;
String username = bean.getUsername();
String password = bean.getPassword();
String searchQuery =
"select * from user where username='"
+ username
+ "' AND password='"
+ password
+ "'";
// "System.out.println" prints in the console; Normally used to trace the process
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: "+searchQuery);
try
{
//connect to DB
currentCon = dbConnection.getConnection();
stmt=currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
// if user does not exist set the isValid variable to false
if (!more)
{
System.out.println("Sorry, you are not a registered user! Please sign up first");
bean.setValid(false);
}
//if user exists set the isValid variable to true
else if (more)
{
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");
System.out.println("Welcome " + firstName);
bean.setfname(firstName);
bean.setlname(lastName);
bean.setValid(true);
}
}
catch (Exception ex)
{
System.out.println("Log In failed: An Exception has occurred! " + ex);
}
//some exception handling
finally
{
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {}
stmt = null;
}
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
return bean;
}
public static List<animalBean> selectListAnimal() throws SQLException {
Statement stmt = null;
List<animalBean> beans = new ArrayList<animalBean>();
try {
currentCon = dbConnection.getConnection();
String animalSearchQuery = "select a.aname ,a.dob, a.gender , a.source, s.sname, a.remark from animal as a , specie as s where a.specie_id = s.specie_id and a.available ='y'";
stmt=currentCon.createStatement();
rs = stmt.executeQuery(animalSearchQuery);
while (rs.next()) {
animalBean bean = new animalBean();
bean.setAnimalName(rs.getString("aname"));
bean.setAnimalDob(rs.getString("dob"));
bean.setAnimalGender(rs.getString("gender"));
bean.setAnimalSource(rs.getString("source"));
bean.setAnimalBreed(rs.getString("sname"));
bean.setAnimalRemark(rs.getString("remark"));
beans.add(bean);
}
} finally {
if (rs != null) try { rs.close(); } catch (SQLException logOrIgnore) {}
if (stmt != null) try { stmt.close(); } catch (SQLException logOrIgnore) {}
if (currentCon != null) try { currentCon.close(); } catch (SQLException logOrIgnore) {}
}
return beans;
}
}
和最后一个JSP页面animalCheck.jsp:
and last the JSP page animalCheck.jsp:
<%@ page language="java"
contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"
import="content.animalBean"
import="content.DAO"
%>
<!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=windows-1256">
<title>Animal list</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<table class="title">
<tr><th>Zoo keeper</th></tr>
</table>
<h1>Animal list</h1>
<center>
<table width="100 % " id='table1' border="1" cellspacing="2" cellpadding="2">
<tr class="tab-highlighted-2">
<td class="tab-highlighted-2" width="15">
<div align="left">Name</div>
</td>
<td class="tab-highlighted-2" width="20">
<div align="left">Age</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">Gender</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">Status</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">Breed</div>
</td>
<td class="tab-highlighted-2" width="15">
<div align="left">Remarks</div>
</td>
</tr>
<c:forEach items="${beans}" var="view">
<tr>
<td>${view.animalName} </td>
<td>${view.animalDob}</td>
<td>${view.animalGender}</td>
<td>${view.animalSource}</td>
<td>${view.animalBreed}</td>
<td>${view.animalRemark}</td>
</tr>
</c:forEach>
</table>
</center>
</body></html>
我已经struggeling这个,因为2天,我检查很多网站和跟随导游很多,但仍然没有为我工作:(
我AP preciate任何形式的帮助。
I've been struggeling on this since 2 days and i checked many websites and followed many guides but still nothing worked for me :(I appreciate any kind of help
推荐答案
您忘了申报JSTL核心标签库。以下内容添加到你的JSP的顶部:
You forgot to declare the JSTL core taglib. Add the following to top of your JSP:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
参见:
- - 包含有关如何安装和使用JSTL信息
- JSP
- Our JSTL wiki page - contains information about how to install and use JSTL
- JSP using MVC and JDBC
- How to avoid Java Code in JSP-Files?
- 您永远不应该声明DB资源
静态
。这不是线程并且容易发生资源泄漏。声明他们非常相同的方法块内为你执行的SQL查询。 - 您在
SQL注入孔登录()
方法。 。 - 您不需要使用
@Page进口
在你的JSP,如果你不使用任何的小脚本的 - 类名是下手大写
- You should never declare DB resources as
static
. This is not threadsafe and is prone to resource leaking. Declare them inside the very same method block as you're executing the SQL query. - You have a SQL injection hole in
login()
method. UsePreparedStatement
. - You don't need to use
@page import
in your JSP if you aren't using any scriptlets. - Classnames are supposed to start with uppercase.
See also:
无关以具体的问题,也有你的code等几个问题:
Unrelated to the concrete problem, there are several other problems in your code:
这篇关于ArrayList的+数据库+的servlet + DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!