按老师的做,最后还是查不到
package com.test.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.bean.Message;
import com.test.conn.ConnectionFactory;
/**
* 列表页面初始化控制
* Servlet implementation class ListServlet
*/
@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ListServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
request.setCharacterEncoding("UTF-8");
String command = request.getParameter("command");
String description = request.getParameter("description");
Connection conn = ConnectionFactory.getConnection();
StringBuilder sql=new StringBuilder("SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM MESSAGE WHERE 1=1");
List<String> paramList=new ArrayList<String>();
if(command !=null && !"".equals(command.trim())){
sql.append(" and COMMAND=?");
paramList.add(command);
}
if(description !=null && !"".equals(description.trim())){
sql.append(" and DESCRIPTION like '%'?'%'");
paramList.add(description);
}
PreparedStatement ps = conn.prepareStatement(sql.toString());
for (int i=0;i<paramList.size();i++){
ps.setString(i+1, paramList.get(i));
}
ResultSet rs = ps.executeQuery();
List<Message> messageList = new ArrayList<Message>();
while(rs.next()){
Message message = new Message();
messageList.add(message);
message.setCommand(rs.getString("COMMAND"));
message.setId(rs.getString("ID"));
message.setDescription(rs.getString("DESCRIPTION"));
message.setContent(rs.getString("CONTENT"));
}
request.setAttribute("messageList", messageList);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
}
结果和老师第一次没有设置编码格式一样