📄 listservlet.java
字号:
package bookstore;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.BookBean;
public class ListServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
ServletContext sc=getServletContext();
String driverClass=sc.getInitParameter("driverClass");
url=sc.getInitParameter("url");
user=sc.getInitParameter("user");
password=sc.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new ServletException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
req.setCharacterEncoding("gb2312");
String condition=req.getParameter("cond");
if(null==condition || condition.equals(""))
{
resp.sendRedirect("search2.jsp");
return;
}
resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
if(condition.equals("all"))
{
rs=stmt.executeQuery("select * from bookinfo");
printBookInfo(out,rs);
out.close();
}
else if(condition.equals("precision"))
{
String title=req.getParameter("title");
String author=req.getParameter("author");
String bookconcern=req.getParameter("bookconcern");
if((null==title || title.equals("")) &&
(null==author || author.equals("")) &&
(null==bookconcern || bookconcern.equals("")))
{
resp.sendRedirect("search2.jsp");
return;
}
StringBuffer sb=new StringBuffer("select * from bookinfo where ");
boolean bFlag=false;
if(!title.equals(""))
{
sb.append("title = "+"'"+title+"'");
bFlag=true;
}
if(!author.equals(""))
{
if(bFlag)
sb.append("and author = "+"'"+author+"'");
else
{
sb.append("author = "+"'"+author+"'");
bFlag=true;
}
}
if(!bookconcern.equals(""))
{
if(bFlag)
sb.append("and bookconcern = "+"'"+bookconcern+"'");
else
sb.append("bookconcern = "+"'"+bookconcern+"'");
}
rs=stmt.executeQuery(sb.toString());
printBookInfo(out,rs);
out.close();
}
else if(condition.equals("keyword"))
{
String keyword=req.getParameter("keyword");
if(null==keyword || keyword.equals(""))
{
resp.sendRedirect("search2.jsp");
return;
}
String strSQL="select * from bookinfo where title like '%"+keyword+"%'";
rs=stmt.executeQuery(strSQL);
printBookInfo(out,rs);
out.close();
}
else
{
resp.sendRedirect("search2.jsp");
return;
}
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
closeResultSet(rs);
closeStatement(stmt);
closeConnection(conn);
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
doGet(req,resp);
}
private void printBookInfo(PrintWriter out,ResultSet rs)
throws SQLException
{
ArrayList<BookBean> bookList=new ArrayList<BookBean>();
while (rs.next())
{
BookBean book = new BookBean(rs.getInt(1), rs.getString(2), rs.getString(3),
rs.getString(4), rs.getString(5),
rs.getFloat(6), rs.getInt(7),
rs.getString(8));
bookList.add(book);
}
out.println("<html><head>");
out.println("<title>图书信息</title>");
out.println("<link href=\"incoming/style.css\" rel=\"stylesheet\" type=\"text/css\">");
out.println("</head><body>");
out.println("<table><tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>查看</th></tr>");
Iterator<BookBean> it=bookList.iterator();
while(it.hasNext())
{
BookBean book=(BookBean)it.next();
String title=book.getTitle();
String author=book.getAuthor();
String bookconcern=book.getBookconcern();
int bookId=book.getId();
float price=book.getPrice();
out.println("<tr><td><a href=\"bookinfo.jsp?id="+bookId+"\">《"+title+"》</a></td><td>"+author+"</td><td>"+bookconcern+"</td><td>"+price+"</td><td><a href=\"bookinfo.jsp?id="+bookId+"\">详细信息</a></td></tr>");
}
out.println("</table><a href=\"index.jsp\">回到主页</a><br></body></html>");
}
private void closeResultSet(ResultSet rs)
{
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
rs=null;
}
}
private void closeStatement(Statement stmt)
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
}
private void closeConnection(Connection conn)
{
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -