SearchArticlesServlet.java
package com.chk.cms.servlet;import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.chk.cms.model.Article;public class SearchArticlesServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");

// 对文章进行分页查询,需要具备几个必要的条件
// 1、从第几条记录开始查询
int offSet = 0;
// 2、每页显示多少条记录
int pageSize = 5;
// 3、总共有多少条记录
int totel; // 从request中获得offset的值 try {
offSet = Integer.parseInt(request.getParameter("offSet"));
} catch (Exception ignore) {
} if (request.getParameter("pageSize") != null) {
request.getSession().setAttribute("pageSize", Integer.parseInt(request.getParameter("pageSize")));
} // 从Http Session中获得pagesize的值
Integer pages = (Integer) request.getSession().getAttribute("pageSize");
if (pages == null) {
request.getSession().setAttribute("pageSize", pageSize);
} else {
pageSize = pages;
} // 查询文章列表
List articleList = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
PreparedStatement psForTotal = null;
ResultSet rs = null;
ResultSet rsForTotal = null;
// 总记录数
int total = 0;
try {
// 查询条件
String title = request.getParameter("title"); Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms", "root", "chengke168");
String sqlCon = "select count(*) from article";
// 查询总记录数
if (title != null) {
sqlCon = "select count(*) from article where title like '%"+title+"%'";
}
psForTotal = conn.prepareStatement(sqlCon);
rsForTotal = psForTotal.executeQuery();
if (rsForTotal.next()) {
total = rsForTotal.getInt(1);
} // 分页查询
String sql = "select * from article limit ?,?";
if (title != null) {
sql = "select * from article where title like '%"+title+"%' limit ?,?";
}
ps = conn.prepareStatement(sql);
ps.setInt(1, offSet);
ps.setInt(2, pageSize);
rs = ps.executeQuery();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (rs.next()) {
Article a = new Article();
a.setId(rs.getInt("id"));
a.setTitle(rs.getString("title"));
a.setSource(rs.getString("source"));
a.setContent(rs.getString("content"));
a.setCreatetime(rs.getTimestamp("createtime"));
a.setUpdatetime(rs.getTimestamp("updatetime"));
a.setDeploytime(rs.getTimestamp("deploytime"));
articleList.add(a);
} } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (rsForTotal != null)
rsForTotal.close();
if (psForTotal != null)
psForTotal.close();
} catch (SQLException e) {
e.printStackTrace();
}
} request.setAttribute("articleList", articleList);
// 总记录数
request.setAttribute("total", total);
// 当前第几页
int currentPage = offSet / pageSize + 1;
request.setAttribute("currentPage", currentPage);
// 总共多少页
int maxPage = total / pageSize;
if (total % pageSize != 0) {
maxPage = maxPage + 1;
}
request.setAttribute("maxPage", maxPage); // forward到atricle_list.jsp
request.getRequestDispatcher("article_list.jsp").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}}
article_list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"><title>My JSP 'article_list.jsp' starting page</title><!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function selectAll(field) {
//根据checkbox框的名称,查询得到所有的checkbox对象
var idCheckboxs = document.getElementsByName("content_id");
for ( var i = 0; i < idCheckboxs.length; i++) {
//判断顶上那个checkbox框的选中状态
if (field.checked) {
idCheckboxs[i].checked = true;
} else {
idCheckboxs[i].checked = false;
}
}
}
//批量删除文章
function del() {
//判断有哪些checkbox框被选中了
var idCheckboxs = document.getElementsByName("content_id");
var url = "DelArticleServlet";
var checkedIds = [];
for ( var i = 0; i < idCheckboxs.length; i++) {
if (idCheckboxs[i].checked) {
checkedIds[checkedIds.length] = idCheckboxs[i].value;
}
}
for ( var i = 0; i < checkedIds.length; i++) {
if (i == 0) {
url = url + "?id=" + checkedIds[i];
} else {
url = url + "&id=" + checkedIds[i];
}
}
alert(url);
//通过GET方式,向后台递交一个请求
window.location = url;
}

function selecetPagesize(field){
window.location = "SearchArticlesServlet?pageSize="+field.value+"&title=${param.title }";
}
</script>
</head><body>
文章列表
<br>
<form action="SearchArticlesServlet" method="post">
<table style="border:1px solid #006600;"width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
文章标题:<input type="text" name="title" value="${param.title }" ><input type="submit" value="查询">
</td>
</tr>
</table>
</form>
<table width="100%" border="1px" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">
<tr style="border:1px solid #006600">
<td colspan="10" align="right"><a href="javascript:del()" style="padding: 10px;">批量删除</a>
</td>
</tr>
<tr bgcolor="#11AAAA" align="center">
<td style="border:1px solid #006600 ;padding: 2px 10px;"><input type="checkbox" id="title_checkbox" onclick="selectAll(this)"></td>
<td style="border:1px solid #006600 ;padding: 2px 10px;">标题</td>
<td style="border:1px solid #006600 ;padding: 2px 10px;">来源</td>
<td style="border:1px solid #006600 ;padding: 2px 10px;">内容</td>
<td style="border:1px solid #006600 ;padding: 2px 10px;">创建时间</td>
<td style="border:1px solid #006600 ;padding: 2px 10px;">更新时间</td>
<td  style="border:1px solid #006600 ;padding: 2px 10px;">时间</td>
<td style="border:1px solid #006600 ;padding: 2px 10px;" colspan="3">操作</td>
</tr>
<c:if test="${not empty articleList }">
<c:forEach items="${articleList }" var="a">
<tr style="border:1px solid #006600" align="center">
<td style="border:1px solid #006600 ;padding: 2px 10px;"><input type="checkbox" name="content_id" value="${a.id }"></td>
<td style="border:1px solid #006600;padding: 2px 10px;">${a.title }</td>
<td style="border:1px solid #006600;padding:2px 10px;">${a.source }</td>
<td style="border:1px solid #006600;padding:2px 10px;">${a.content }</td>
<td style="border:1px solid #006600;padding:2px  10px;">${a.createtime }</td>
<td style="border:1px solid #006600;padding: 2px 10px;">${a.updatetime }</td>
<td style="border:1px solid #006600;padding: 2px 10px;">${a.deploytime }</td>
<td style="border:1px solid #006600;padding: 2px 10px;"><a href="add_article.jsp" title="发布文章">发布</a></td>
<td style="border:1px solid #006600;padding: 2px 10px;"><a href="DelArticleServlet?id=${a.id }" title="删除文章">删除</a></td>
<td style="border:1px solid #006600;padding: 2px 10px;"><a href="OpenUpdateArticleServlet?id=${a.id }" title="修改文章">修改</a></td> </tr>
</c:forEach>
<br>
</c:if>
</table>
<br>
<c:if test="${empty articleList}">
<tr>没有文章可以显示
</tr>
</c:if>
<table style="border:1px solid #006600;"width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" width="40%">共有 ${total }   记录,当前第  ${currentPage }   页,共有  ${maxPage }   页</td>
<td align="right" width="60%" vAlign="center" noWrap>
<a href="SearchArticlesServlet?offSet=0&title=${param.title }">首页</a>
<a href="SearchArticlesServlet?offSet=${(currentPage-2 lt 0 ? 0 : (currentPage-2))*pageSize }&title=${param.title }" >前页</a>
<c:forEach begin="1" end="${maxPage }" step="1" var="i">
<c:if test="${currentPage eq i }">
<font color="red">${i }</font>
</c:if>
<c:if test="${currentPage ne i}">
<a href="SearchArticlesServlet?offSet=${(i-1)*pageSize }&title=${param.title }"> ${i }</a>
</c:if>
</c:forEach>
<a href="SearchArticlesServlet?offSet=${(currentPage ge maxPage ? (currentPage-1):currentPage)*pageSize }&title=${param.title }">后页</a>
<a href="SearchArticlesServlet?offSet=${(maxPage-1)*pageSize  }&title=${param.title }" >尾页</a>

<select name="pageSize" onchange="selecetPagesize(this)">
<c:forEach begin="5" end="50" step="5" var="i">
<option value="${i }" <c:if test="${i eq pageSize }">selected</c:if> >${i }</option>
</c:forEach>
</select></td>
</tr>
</table>
</body>
</html>
问题是:因为我的数据库的内容是中文的,所以当我用中午作为查询条件的时候,在条件框中输入中文点击查询,可以搜索到内容,但是当我在点击分页的页码的时候,搜索条件框中文章就变成乱码了,也就搜索不到内容了,这因该怎样解决

解决方案 »

  1.   

    http://blog.csdn.net/tkd03072010/article/details/6667689 参考一下这个
      

  2.   

    看到你用limit方法分页了,应该是mysql。安装mysql时字符编码选的什么?最好选择utf-8,我猜你没选,用的默认的lin...西欧编码方式。所以会乱码。
      

  3.   

    碰到乱码直接debug,看看从哪一步开始出现乱码的,然后找对应解决方法。有时候是后台从页面取参数乱码,有时候是经错数据库乱了,有时候从后台是好的到页面显示又乱了。
      

  4.   

    param是从哪里来的啊???????????????