📄 b064f4f7070a001c1ae389e389b64382
字号:
import bean.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Class <b>SearchController</b> contains
* the servlet controller functionality for processing
* user search requests.
*
*
* @author ProjectGroup
* @version 1.0.0
*/
public class SearchController extends Controller {
/**
* SearchController doPost method. This is can be called
* by <b>Controller</b> superclass' doGet() method.
* @param req HttpServletRequest servlet request object
* @param res HttpServletResponse servlet response object
* @throws ServletException
* @throws IOException
*/
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Get the search parameters.
String action = req.getParameter("action");
String pterms = req.getParameter("str");
HttpSession session=req.getSession();
// Redirect the user back to the index page if no terms are selected
if (pterms.equals("")) {
res.sendRedirect("/e-commerce/index.jsp");
} else {
// Get the searchby parameter;
String searchby = req.getParameter("searchby");
// Build the sql statement based on what is to be searched on (author or title).
String sql = "select * from booktitle where ";
if (searchby.equals("author")) {
sql += "B_Author like '%" + pterms + "%'";
}
if (searchby.equals("title")) {
sql +="B_Title like '%"+pterms+"%'";
}
else if(searchby.equals("Catalogue")){
sql +="B_ISBN=any(select isbn from bt_belong_categ where Categ_num=any(select Categ_num from Categories where Categ_name likes '%"+pterms+"%'))";
}
// The results of the search are to be stored in a BookTitleSet object.
BookTitleSet results = new BookTitleSet();
// Execute the search and fill the BookTitleSet with the results.
try {
ResultSet r = null;
// Get an instance of the singleton DBWrapper object.
DBWrapper db = DBWrapper.Instance();
// Run the query.
r = db.runQuery( sql );
// Fill the BookTitleSet with BookTitles.
while (r.next()) {
BookTitle tempTitle = new BookTitle( r.getString( "B_Title" ), r.getString( "B_Author" ),
r.getString( "B_ISBN" ), r.getString( "B_Edition" ),r.getString("pub_num"),r.getString("B_DatePublished"),
r.getDouble( "B_Price" ), r.getDouble( "B_disount" ),r.getString("B_Description"),r.getInt("qtyinstock"),
r.getString( "Cata_num" ));
results.addBookTitle( tempTitle );
}
} catch ( Exception e ) {
// Send this exception to the standard error page.
sendErrorRedirect(req, res, e);
}
/**
* Redirect to the search.jsp page to display the search results.
* The search results are stored in the HttpServletRequest's
* session.
*/
try {
req.getSession().setAttribute("results", results);
//res.sendRedirect("/e-commerce/search.jsp");
} catch( Exception e ){
sendErrorRedirect(req, res, e);
}
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -