JavaWEB查詢資料庫資訊(JSTL)
阿新 • • 發佈:2019-02-18
1、Servlet原始碼
實現資料庫查詢,並將資料封裝到javabean中,將封裝的資料新增到request作用域中,然後用JSTL標籤遍歷request作用域,實現資料查詢操作。
注:javabean中封裝的資料屬性名要和資料庫表中的列明相同,不然查詢為空。
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dataBean.ProductInfoBean;
import dataDAO.SearchData;
public class SearchInfoServlet extends HttpServlet {
/**
* 以批號、id號、是否合格為條件,對資料庫進行查詢,將結果集封裝到List<ProductInfoBean>,通過session儲存查詢返回的資料,頁面轉發到查詢頁。
*
* */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//獲取jsp頁面提交的搜尋引數
String searchbatch=new String(request.getParameter("searchBatch").getBytes("iso-8859-1"),"utf-8");
String searchid=new String(request.getParameter("searchId").getBytes("iso-8859-1"),"utf-8");
String testresult=new String(request.getParameter("testResult" ).getBytes("iso-8859-1"),"utf-8");
try{
ProductInfoBean probean=new ProductInfoBean();
probean.setBatch_Number(searchbatch);
probean.setTestid(searchid);
probean.setResult(testresult);
List<ProductInfoBean> list= SearchData.SearchBatchNumber(probean);
request.setAttribute("list", list);
request.getRequestDispatcher("/Search.jsp").forward(request, response);
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
2、JSP頁面程式碼(JSTL標籤)