List<对象>数据输出到JSP页面及利用过滤器在页面跳转前进行数据查询,输出到页面上和我的DBUtil工具类(改)
1、今天最近做一个小项目,把上传文件信息发到jsp页面,在上面显示,把数据查出来后存到List里面后发现打印的一串地址,有点搞不清原因,找了好一会才发现用进行输出要把List<对象>内的对象toString()进行重写,这个以后要注意。
@Override
public String toString(){
return "codeid:"+getCodeid()+","+"codename:"+getCodename()+","+"codepath:"+getCodepath()+","+"codedesc:"+getCodedesc()+","+"isrank:"+getIsrank();
}
jsp页面利用jstl的forEach进行循环输出
<c:forEach items="${sessionScope.codes}" var="s">
<tr>
<td align="center"><c:out value="${s.codeid}"></c:out></td>
<td align="center"><a href="${s.codepath}"><c:out value="${s.codename}"></c:out></a></td>
<td align="center"><c:out value="${s.codedesc}"></c:out></td>
<td align="center" width="200px"><a class="submit_btn" href="edit.jsp"><span
class="icon-newtab"></span> 修改</a><a class="grey_btn" href="DeleteServlet"><span
class="icon-remove"></span>
删除</a></td>
</tr>
2、项目进行的过程中,我想在页面出现前让数据查询完一起和页面出现,但我不是把数据输出在首页(输出首页可以利用web.xml进行配置先执行servlet),在网上找了一会发现大部分都是说使用ajax,struts等,我并不是特别会,于是想到使用过滤器,在点击admin.jsp后,拦截跳转请求,在过滤器的doFilter()内进行查询,在解除拦截后随着response把数据发到admin.jsp页面进行显示
过滤器
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest reqRequest = (HttpServletRequest) request;
HttpServletResponse resonResponse = (HttpServletResponse) response;
HttpSession session = reqRequest.getSession();
ICodeDao codeDao = new CodeDaoImpl();
List<Code> codes = (List<Code>) codeDao.select();
for (Code code :codes){
System.out.println(code);
}
session.setAttribute("codes",codes);
filterChain.doFilter(request,response);
}
我的DBUtil工具类
package com.tx.test.util;
import java.sql.*;
public class DBUtil {
static Connection con = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "root");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static int executeUp(String sql, Object[] params) {
//0:表示已经存在该对象数据
//1:表示成功
//-1:系统error
try {
circulate(sql, params);
//ps.executeUpdate():返回值为sql语句影响的行数 --> 0:表示操作失效
int count = ps.executeUpdate();
if (count > 0) {
return 1;
} else {
return 0;
}
} catch (SQLException e) {
e.printStackTrace();
return -1;
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
DBCloseCP(con, ps);
}
}
//循环
private static void circulate(String sql, Object[] params) {
try {
//sql为传过来的数据库命令
ps = getConnection().prepareStatement(sql);
if (params != null) {
//根据数组params.Length为?的个数所决定
for (int i = 0; i < params.length; i++) {
//占位符使用Object所有数据都可以传,i从0开始,但占位符第一位为1,所以要加1
//params[i]为数组内的数据
ps.setObject(i + 1, params[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//查询
public static ResultSet executeSelect(String sql, Object[] params) {
try {
circulate(sql, params);
//ps.executeUpdate():返回值为sql语句影响的行数 --> 0:表示操作失效
return ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//关闭 Connection,PreparedStatement关闭
public static void DBCloseCP(Connection con, PreparedStatement ps) {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭 Connection,PreparedStatement,ResultSet关闭
public static void DBCloseCPR(Connection con, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}