解决服务端接收数据乱码问题。服务器默认采用 ISO8859-1 编码响应内容。
// 1req.setCharacterEncoding("utf-8");// 2 byte[] bytes = req.getParameter("username").getBytes("iso-8859-1"); System.out.println("username:" + new String(bytes,0,bytes.length,"utf-8"));
解决服务端发送到客户端数据乱码问题。
// 1 text/html,响应数据类型; resp.setContentType("text/html;charset=utf8;"); // 2 resp.setCharacterEncoding("utf-8"); resp.setHeader("Content-type","text/html;charset=utf8");
单元测试。
src 同级目录创建 test 文件夹,右键 -> Mark Directory as ->Sources Root,文件名采用测试的模块名 + Test。
转发。转发的作用在服务器端,将请求发送给服务器上的其他资源,以共同完成一次请求的处理。使用forward跳转时,是在服务器内部跳转,地址栏不发生变化,属于同一次请求。
request.getRequestDispatcher(“/目标URL-pattern”).forward(request, response);// 存数据request.setAttribute(key,value);// 取数据request.getAttribute(key);
重定向。重定向作用在客户端,客户端将请求发送给服务器后,服务器响应给客户端一个新的请求地址,客户端重新发送新请求。
重定向可以指向任何的资源,包括当前应用程序中的其他资源、同一个站点上的其他应用程序中的资源、其他站点的资源。
response.sendRedirect(“目标URI”);// 带数据response.sendRedirect(“目标URI?username=tom&ps=123”);// 接收数据request.getParameter(“username”);
servlet 用户管理系统。
目录结构。
public class AddUserServlet extends HttpServlet {
private IUserService userService = new UserServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String age = req.getParameter("age"); String sex = req.getParameter("sex"); User user = new User(); user.setUsername(username); user.setPassword(password); user.setAge(Integer.valueOf(age)); user.setSex(Integer.valueOf(sex)); int i = userService.AddUser(user); if (i == 0) {
resp.getWriter().write("添加失败"); } else {
resp.getWriter().write("添加成功"); resp.getWriter().write("查询所有用户"); } }}public class DeleteUserServlet extends HttpServlet {
private IUserService userService = new UserServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf8"); String username = req.getParameter("username"); int i = userService.DeleteUser(username); if (i == 0) {
resp.getWriter().write("删除失败"); } else {
resp.getWriter().write("删除成功"); resp.getWriter().write("查询所有用户"); } }}public class GetUserListServlet extends HttpServlet {
private IUserService userService = new UserServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf8"); System.out.println("查询所有的用户:" + req); List userList = userService.getUserList();