黑马程序员技术交流社区
标题:
注册
[打印本页]
作者:
旺仔奶糖
时间:
2019-9-25 17:43
标题:
注册
package cn.itcast.travel.web.servlet;
import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.Userservice;
import cn.itcast.travel.service.impl.UserserviceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;
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.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Set;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前台输入的验证码
String check = request.getParameter("check");
//获取后台生成的验证码
String checkcode_server =(String) request.getSession().getAttribute("CHECKCODE_SERVER");
//保证验证码只用一次
request.getSession().removeAttribute("CHECKCODE_SERVER");
//两者对比
if(checkcode_server==null||!checkcode_server.equalsIgnoreCase(check)){
//验证码错误,将数据存入ResultInfo对象中
ResultInfo info =new ResultInfo();
info.setFlag(false);
info.setErrorMsg("验证码错误!");
//序列化为json格式,写回页面
ObjectMapper mapper =new ObjectMapper();
String json = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
return;
}
//验证码正确,获取所有数据并封装对象
Map<String, String[]> map = request.getParameterMap();
System.out.println(map.size());
User user =new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(user.getUsername()+"huhu");
Userservice service =new UserserviceImpl();
User u=service.login(user);
System.out.println(u+"ll");
ResultInfo info =new ResultInfo();
if(u==null){
info.setFlag(false);
info.setErrorMsg("用户名或者密码错误");
}
if(u!=null&&!"Y".equals(u.getStatus())){
info.setFlag(false);
info.setErrorMsg("您尚未激活,请先激活");
}
if(u!=null&&"Y".equals(u.getStatus())){
request.getSession().setAttribute("user",u);
info.setFlag(true);
}
ObjectMapper mapper =new ObjectMapper();
response.setContentType("application/json;charset=utf-8");
String json = mapper.writeValueAsString(info);
System.out.println(json);
response.getWriter().write(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2