⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchcontroller.java

📁 图书馆检索系统
💻 JAVA
字号:
import java.util.*;import java.io.*;import java.net.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;import library.*;public class SearchController extends Controller {  public void doPost (HttpServletRequest req, HttpServletResponse res)          throws ServletException, IOException {    String pterms = req.getParameter("str");    // Redirect back to the index page if no terms are selected    if (pterms.equals("")) {      res.sendRedirect("/library/index.jsp");    } else {      String action = req.getParameter("action");      String searchby = req.getParameter("searchby");            // Make sure to replace any characters that      // could mess up the SQL statement.      String terms = StringReplace.replace(pterms, "\'", "\'\'"); // Apostrophe      // 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 += "author like '%" + terms + "%'";      } else {        sql += "name like '%" + terms + "%'";      }      Vector results = new Vector();      // Execute the search and fill a vector with search results.      try {        BookTitle searchResults = new BookTitle(sql);        while (searchResults.getNext()) {          SearchResult sr = searchResults.getSearchResult();          results.add(sr);        }      } catch (SQLException e) {        System.err.println(e.toString());        sendErrorRedirect(req, res, e);      }      // Redirect to the search.jsp page to      // display the search results.      req.getSession().setAttribute("results", results);      String url = "/library/search.jsp?start=0&searchby=" + searchby +              "&str=" + URLEncoder.encode(pterms, "UTF-8");      res.sendRedirect(url);      return;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -