?? petscontrol.java~88~
字號(hào):
package com.richard.control;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.richard.service.Factory;
import com.richard.dao.InterfaceOfOwners;
import com.richard.dao.InterfaceOfPets;
import com.richard.dao.InterfaceOfViewOfPets;
import com.richard.dto.*;
public class PetsControl extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setCharacterEncoding("GB2312");
request.setCharacterEncoding("GB2312");
response.setContentType(CONTENT_TYPE);
String param = request.getParameter("param"); //接收param參數(shù),用于判斷servlet處理何種操作
HttpSession objSession = request.getSession();
if (param != null) {
if (param.equals("query")) {
InterfaceOfViewOfPets objViewOfPets = Factory.createViewOfPets();
String pageNumber = request.getParameter("pageNumber");
if (pageNumber != null) {
if (pageNumber.equals("firstPage")) {
ArrayList objArrayList = new ArrayList();
objArrayList = objViewOfPets.ViewOfPetsQueryAll(15, 1);
objSession.setAttribute("petsTotalPage",
objViewOfPets.totalPages());
request.setAttribute("queryResult", objArrayList); //將返回的ArrayList傳遞給queryemployee.jsp頁(yè)面
request.setAttribute("currentPage", 1);
RequestDispatcher objDispatcher = request.
getRequestDispatcher(
"context/querypets.jsp");
objDispatcher.forward(request, response);
}
}
}
if (param.equals("querybydetail")) { //按條件查詢(xún)
String petsName = new String(request.getParameter("petsname").
getBytes("ISO8859_1")); //字符轉(zhuǎn)換
String petsType = new String(request.getParameter("petstype").
getBytes("ISO8859_1"));
String petsOwner = new String(request.getParameter("petsowner").
getBytes("ISO8859_1"));
String detailpageNumber = request.getParameter(
"detailpageNumber");
if (petsName.equals("") && petsType.equals("") &&
petsOwner.equals("")) { //當(dāng)所有查詢(xún)條件都為空時(shí)則顯示所有的資料
} else {
String command = "and pets_name like'%" + petsName +
"%' and types_name like'%" + petsType +
"%' and owners_name like '%" + petsOwner +
"%'";
ArrayList objArrayList = new ArrayList();
objArrayList = Factory.createViewOfPets().ViewOfPetsQuery(
15, 1, command);
objSession.setAttribute("petsDetailTotalPage",
Factory.createViewOfPets().
totalPages());
request.setAttribute("petsname", petsName);
request.setAttribute("petstype", petsType);
request.setAttribute("petsowner", petsOwner);
request.setAttribute("queryDetailResult", objArrayList); //將返回的ArrayList傳遞給queryemployee.jsp頁(yè)面
request.setAttribute("currentDetailPage", 1);
RequestDispatcher objDispatcher = request.
getRequestDispatcher(
"context/querydetailpets.jsp");
objDispatcher.forward(request, response);
}
}
if (param.equals("addPets")) { //添加新的寵物資料
ArrayList ownersArrayList = Factory.createOwners().queryAll(); //獲取所有寵物主人的姓名
request.setAttribute("ownersInfo", ownersArrayList);
ArrayList typesArrayList = Factory.createTypes().typesQueryAll(); //獲取所有寵物類(lèi)別
request.setAttribute("typesInfo", typesArrayList);
RequestDispatcher objDispatcher = request.getRequestDispatcher(
"context/addpets.jsp");
objDispatcher.forward(request, response);
}
if (param.equals("addPetsSave")) { //處理新增保存
String petName=request.getParameter("petName");
String petsBirthday=request.getParameter("petsBirthday");
String petsType=request.getParameter("petsType");
String petsOwner=request.getParameter("petsOwner");
Pets petsBean=new Pets();
petsBean.setBirth_date(petsBirthday);
petsBean.setName(petName);
petsBean.setType_id(Factory.createTypes().nameForId(petsType));
petsBean.setOwner_id(Factory.createOwners().nameForId(petsOwner));
Factory.createPets().petsInsert(petsBean);
response.sendRedirect("context/addpets.jsp");
}
}
if (param.equals("querydetail")) {
String detailpageNumber = request.getParameter("detailpageNumber");
if (detailpageNumber != null) {
if (detailpageNumber.equals("firstPage")) { //處理?xiàng)l件查詢(xún)的翻頁(yè)事件
ArrayList objArrayList = new ArrayList();
String command = "and pets_name like'%" +
new String(request.getParameter("petsname").
getBytes("ISO8859_1")) +
"%' and types_name like'%" +
new String(request.getParameter("petstype").
getBytes("ISO8859_1")) +
"%' and owners_name like '%" +
new String(
request.getParameter("petsowner").
getBytes("ISO8859_1")) + "%'";
objArrayList = new ArrayList();
objArrayList = Factory.createViewOfPets().ViewOfPetsQuery(
15, 1, command);
objSession.setAttribute("petsDetailTotalPage",
Factory.createViewOfPets().
totalPages());
request.setAttribute("queryDetailResult", objArrayList); //將返回的ArrayList傳遞給queryemployee.jsp頁(yè)面
request.setAttribute("currentDetailPage", 1);
request.setAttribute("petsname",
new String(request.
getParameter("petsname").
getBytes("ISO8859_1")));
request.setAttribute("petstype",
new String(request.
getParameter("petstype").
getBytes("ISO8859_1")));
request.setAttribute("petsowner",
new String(request.
getParameter("petsowner").
getBytes("ISO8859_1")));
RequestDispatcher objDispatcher = request.
getRequestDispatcher(
"context/querydetailpets.jsp");
objDispatcher.forward(request, response);
}
}
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -