public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String req=request.getParameter("user");
String url=request.getParameter("url");
if(req.equals("addUser"))
{
String idstr=request.getParameter("userUp");
//更新用户
if(idstr.length()>=1)
{
User user=new User();
int id=Integer.parseInt(idstr);
String uname=request.getParameter("uname");
String[] values=request.getParameterValues("ulikes");
String ulikes=userImpl.joinString(values);
String uaddress=request.getParameter("uaddress");
user.setId(id);
user.setName(uname);
user.setLikes(ulikes);
user.setAddress(uaddress);
userImpl.upDate(user);
}else{
//添加用户
User user=new User();
String uname=(String)request.getParameter("uname");
String[] uidvalues=request.getParameterValues("ulikes");
String ulikes =userImpl.joinString(uidvalues);
String uaddress=request.getParameter("uaddress");
user.setName(uname);
user.setLikes(ulikes);
user.setAddress(uaddress);
userImpl.insert(user);
}
}else if(req.equals("queryUser"))
{ //查询用户
User user=null;
String id=request.getParameter("uid");
if(id.length()!=0)
{ //使用id查询单用户
user=userImpl.select(Integer.parseInt(id));
request.setAttribute("user", user);
request.setAttribute("usernumber",1);
RequestDispatcher dispat=request.getRequestDispatcher("/queryUser.jsp");
dispat.forward(request, response);//响应
}else
{
//模糊查询
String name=request.getParameter("uname");
String[] values=request.getParameterValues("ulikes");
String address=request.getParameter("uaddress");
String likes=userImpl.joinString(values);//字符串数组转换成字符
user=new User();
user.setName(name);
user.setLikes(likes);
user.setAddress(address);
List<User> users=userImpl.select(user);
request.setAttribute("usernumber", 2);
request.setAttribute("users", users);
RequestDispatcher dispat=request.getRequestDispatcher("/queryUser.jsp");
dispat.forward(request, response);//响应
}
}
} |