serlvet中的代码
package com.entor.servlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.entity.User;
import com.entor.service.UserService;
import com.entor.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String opType = request.getParameter("opType");
switch (opType){
case "login":
login(request,response);
break;
case "logout":
logout(request,response);
break;
case "add":
add(request,response);
break;
case "addMore":
addMore(request,response);
break;
case "deleteById":
deleteById(request,response);
break;
case "deleteByIds":
deleteByIds(request,response);
break;
case "update":
update(request,response);
break;
case "queryById":
queryById(request,response);
break;
case "queryByPage":
queryByPage(request,response);
break;
default:
queryByPage(request,response);
break;
}
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserService userService = new UserServiceImpl();
User user = userService.login(username, password);
//返回数据给客户端
JSONObject jo = new JSONObject();
if(user!=null){
jo.put("code",0);
jo.put("msg","登录成功");
}else{
jo.put("code",-1);
jo.put("msg","用户名或者密码错误");
}
String json = JSON.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端传递的参数值
String name = request.getParameter("name");
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String birthday = request.getParameter("birthday");
//封装对象
User user = new User();
user.setName(name);
user.setUsername(username);
user.setPassword(password);
user.setSex(Integer.parseInt(sex));
user.setAge(Integer.parseInt(age));
try {
user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
} catch (ParseException e) {
e.printStackTrace();
}
//调用业务层对象
UserService userService = new UserServiceImpl();
userService.add(user);
//返回数据给客户端
JSONObject jo = new JSONObject();
jo.put("code",0);
jo.put("msg","新增成功");
String json = JSON.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
public void addMore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
UserService userService = new UserServiceImpl();
int num = userService.deleteById(Integer.parseInt(id));
//返回数据给客户端
JSONObject jo = new JSONObject();
if(num>0){
jo.put("code",0);
jo.put("msg","删除成功");
}else{
jo.put("code",-1);
jo.put("msg","删除失败");
}
String json = JSON.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ids = request.getParameter("ids");
UserService userService = new UserServiceImpl();
int num = userService.deleteByIds(ids);
//返回数据给客户端
JSONObject jo = new JSONObject();
if(num>0){
jo.put("code",0);
jo.put("msg","删除成功");
}else{
jo.put("code",-1);
jo.put("msg","删除失败");
}
String json = JSON.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取客户端传递的参数值
String id = request.getParameter("id");
String name = request.getParameter("name");
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String birthday = request.getParameter("birthday");
//封装对象
User user = new User();
user.setId(Integer.parseInt(id));
user.setName(name);
user.setUsername(username);
user.setPassword(password);
user.setSex(Integer.parseInt(sex));
user.setAge(Integer.parseInt(age));
try {
user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
} catch (ParseException e) {
e.printStackTrace();
}
//调用业务层对象
UserService userService = new UserServiceImpl();
userService.update(user);
//返回数据给客户端
JSONObject jo = new JSONObject();
jo.put("code",0);
jo.put("msg","修改成功");
String json = JSON.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
public void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void queryByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String limit = request.getParameter("limit");
String page = request.getParameter("page");
UserService userService = new UserServiceImpl();
List<User> list = userService.queryByPage(Integer.parseInt(page), Integer.parseInt(limit));
//拼装客户端接收的数据格式
JSONObject jo = new JSONObject();
jo.put("code",0);
jo.put("msg","");
jo.put("count",userService.getTotal());
jo.put("data",list);
//把对象转换成json格式字符串
String json = JSONObject.toJSONString(jo);
System.out.println(json);
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
}
}