|
1 | 1 | package servlet;
|
2 | 2 |
|
3 | 3 | import cn.itcast.commons.CommonUtils;
|
4 |
| -import cn.itcast.servlet.*; |
5 | 4 | import cn.itcast.servlet.BaseServlet;
|
6 | 5 | import domain.Customer;
|
7 | 6 | import domain.PageBean;
|
|
12 | 11 | import javax.servlet.http.HttpServletResponse;
|
13 | 12 | import java.io.IOException;
|
14 | 13 | import java.io.UnsupportedEncodingException;
|
15 |
| -import java.util.List; |
16 | 14 |
|
17 | 15 | /**
|
18 | 16 | * Created by codingBoy on 16/10/23.
|
19 | 17 | */
|
20 | 18 | public class CustomerServlet extends BaseServlet {
|
| 19 | + |
21 | 20 | private CustomerService customerService = new CustomerService();
|
22 | 21 |
|
23 | 22 | public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
@@ -52,23 +51,21 @@ public String findAll(HttpServletRequest request, HttpServletResponse response)
|
52 | 51 | * 如果pc参数不存在,说明pc=1
|
53 | 52 | * 如果pc参数存在,需要转换成int类型
|
54 | 53 | */
|
55 |
| - int pc=getPc(request); |
| 54 | + int pc = getPc(request); |
56 | 55 |
|
57 |
| - int pr=10;//给定pr的值,每页10行纪录 |
| 56 | + int pr = 10;//给定pr的值,每页10行纪录 |
58 | 57 |
|
59 |
| - PageBean<Customer> pb= customerService.findAll(pc,pr); |
| 58 | + PageBean<Customer> pb = customerService.findAll(pc, pr); |
60 | 59 | pb.setUrl(getUrl(request));
|
61 | 60 |
|
62 |
| - request.setAttribute("pb",pb); |
| 61 | + request.setAttribute("pb", pb); |
63 | 62 |
|
64 | 63 | return "f:/list.jsp";
|
65 | 64 | }
|
66 | 65 |
|
67 |
| - private int getPc(HttpServletRequest request) |
68 |
| - { |
69 |
| - String value=request.getParameter("pc"); |
70 |
| - if (value==null||value.trim().isEmpty()) |
71 |
| - { |
| 66 | + private int getPc(HttpServletRequest request) { |
| 67 | + String value = request.getParameter("pc"); |
| 68 | + if (value == null || value.trim().isEmpty()) { |
72 | 69 | return 1;
|
73 | 70 | }
|
74 | 71 | return Integer.parseInt(value);
|
@@ -118,65 +115,59 @@ public String delete(HttpServletRequest request, HttpServletResponse response) t
|
118 | 115 |
|
119 | 116 | public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
120 | 117 |
|
121 |
| - Customer customer=CommonUtils.toBean(request.getParameterMap(),Customer.class); |
| 118 | + Customer customer = CommonUtils.toBean(request.getParameterMap(), Customer.class); |
122 | 119 |
|
123 | 120 | // System.out.println(getUrl(request));
|
124 |
| - customer=encoding(customer); |
| 121 | + customer = encoding(customer); |
125 | 122 |
|
126 |
| - int pc=getPc(request); |
127 |
| - int pr=10; |
| 123 | + int pc = getPc(request); |
| 124 | + int pr = 10; |
128 | 125 |
|
129 |
| - PageBean<Customer> pb= customerService.query(customer,pc,pr); |
| 126 | + PageBean<Customer> pb = customerService.query(customer, pc, pr); |
130 | 127 |
|
131 | 128 | pb.setUrl(getUrl(request));
|
132 | 129 |
|
133 |
| - request.setAttribute("pb",pb); |
| 130 | + request.setAttribute("pb", pb); |
134 | 131 | return "/list.jsp";
|
135 | 132 |
|
136 | 133 | }
|
137 | 134 |
|
138 | 135 | private Customer encoding(Customer customer) throws UnsupportedEncodingException {
|
139 |
| - String name=customer.getName(); |
140 |
| - String gender=customer.getGender(); |
141 |
| - String phone=customer.getPhone(); |
142 |
| - String email=customer.getEmail(); |
143 |
| - |
144 |
| - if (name!=null&&!name.trim().isEmpty()) |
145 |
| - { |
146 |
| - name=new String(name.getBytes("ISO-8859-1"),"utf-8"); |
| 136 | + String name = customer.getName(); |
| 137 | + String gender = customer.getGender(); |
| 138 | + String phone = customer.getPhone(); |
| 139 | + String email = customer.getEmail(); |
| 140 | + |
| 141 | + if (name != null && !name.trim().isEmpty()) { |
| 142 | + name = new String(name.getBytes("ISO-8859-1"), "utf-8"); |
147 | 143 | customer.setName(name);
|
148 | 144 | }
|
149 |
| - if (gender!=null&&!gender.trim().isEmpty()) |
150 |
| - { |
151 |
| - gender=new String(gender.getBytes("ISO-8859-1"),"utf-8"); |
| 145 | + if (gender != null && !gender.trim().isEmpty()) { |
| 146 | + gender = new String(gender.getBytes("ISO-8859-1"), "utf-8"); |
152 | 147 | customer.setGender(gender);
|
153 | 148 | }
|
154 |
| - if (phone!=null&&!phone.trim().isEmpty()) |
155 |
| - { |
156 |
| - phone=new String(phone.getBytes("ISO-8859-1"),"utf-8"); |
| 149 | + if (phone != null && !phone.trim().isEmpty()) { |
| 150 | + phone = new String(phone.getBytes("ISO-8859-1"), "utf-8"); |
157 | 151 | customer.setPhone(phone);
|
158 | 152 | }
|
159 |
| - if (email!=null&&!email.trim().isEmpty()) |
160 |
| - { |
161 |
| - email=new String(email.getBytes("ISO-8859-1"),"utf-8"); |
| 153 | + if (email != null && !email.trim().isEmpty()) { |
| 154 | + email = new String(email.getBytes("ISO-8859-1"), "utf-8"); |
162 | 155 | customer.setEmail(email);
|
163 | 156 | }
|
164 | 157 | return customer;
|
165 | 158 | }
|
166 | 159 |
|
167 |
| - private String getUrl(HttpServletRequest request) |
168 |
| - { |
169 |
| - String contextPath=request.getContextPath(); |
170 |
| - String serveltPath=request.getServletPath(); |
171 |
| - String queryString=request.getQueryString(); |
| 160 | + private String getUrl(HttpServletRequest request) { |
| 161 | + String contextPath = request.getContextPath(); |
| 162 | + String servletPath = request.getServletPath(); |
| 163 | + String queryString = request.getQueryString(); |
172 | 164 |
|
173 |
| - if (queryString.contains("&pc=")) |
174 |
| - { |
175 |
| - int index=queryString.lastIndexOf("&pc="); |
176 |
| - queryString=queryString.substring(0,index); |
| 165 | + if (queryString.contains("&pc=")) { |
| 166 | + int index = queryString.lastIndexOf("&pc="); |
| 167 | + queryString = queryString.substring(0, index); |
177 | 168 | }
|
178 | 169 |
|
179 |
| - return contextPath+serveltPath+"?" +queryString; |
| 170 | + return contextPath + servletPath + "?" + queryString; |
180 | 171 | }
|
181 | 172 | }
|
182 | 173 |
|
0 commit comments