把结果集置入request,然后在转向页面中取出并显示String str = rs.getString("main") ;
request.setAttribute("str", str) ;转向页面
String str = (String)request.getAttribute("str") ;out.print(str) ;
request.setAttribute("str", str) ;转向页面
String str = (String)request.getAttribute("str") ;out.print(str) ;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;public class SearchAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
SearchForm seaFrm = (SearchForm)form;
String strKeyWord = seaFrm.getKeyWord();
if(strKeyWord.length() < 1)
{
System.out.println("return False strKeyWord is empty!");
return mapping.findForward("False");
} DataSource ds;
Connection cn = null;
try
{
ds = getDataSource(request);
cn = ds.getConnection();
Statement stmt = cn.createStatement();
// sql语句中的"binary" 是为了解决查询中文时mysql查询不准的问题
ResultSet rs = stmt.executeQuery("select * from content where binary content like '%" + strKeyWord + "%'"); ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ArrayList rows = new ArrayList();
while(rs.next())
{
HashMap row = new HashMap();
for(int i = 1; i <= columnCount; i++)
{
String name = rsmd.getColumnName(i);
String strGbk = (String)rs.getObject(i);
// 转换字符编码到简体中文
strGbk = new String(strGbk.getBytes("ISO8859_1"), "GBK");
// 替换成网页中使用的换行符 jsp中要把bean:write 的filter属性设置成flase
strGbk = strGbk.replaceAll("\r\n","<br>");
row.put(name, strGbk);
} rows.add(row);
}
request.setAttribute("queryResult", rows); rs.close();
stmt.close();
}
catch(SQLException sqle)
{
request.setAttribute("strError", sqle.getMessage());
return mapping.findForward("False");
} try{cn.close();}catch(SQLException sqle){System.out.println(sqle.getMessage());} return mapping.findForward("Success");
}}<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %><!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=gb2312">
<title>测试Struts状态</title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-size: 36px;
font-weight: bold;
}
.style2 {color: #FF6600}
-->
</style>
</head><body><p> </p>
<p align="center" class="style1 style2">搜索成功</p><logic:iterate id="rs" name="queryResult">
<bean:write name="rs" property="flag" />
<p>
<bean:write name="rs" property="title" />
<p>
<bean:write name="rs" property="content" filter="false" />
<p>
</logic:iterate></body>
</html>