UserServlet页面抽取
package cn.itcast.travel.web.servlet;
import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.UserServlce;
import cn.itcast.travel.service.impl.UserServlceImpl;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
// 声明一个业务对象
private UserServlce servlce = new UserServlceImpl();
/**
* 注册功能
*/
public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
// 验证码校验
String check = request.getParameter("check");
// 从sesion中获取验证吗
HttpSession session = request.getSession();
String server = (String) session.getAttribute("CHECKCODE_SERVER");
session.removeAttribute("CHECKCODE_SERVER");//为了验证码只能使用一次
// 比较
if (server==null||!server.equalsIgnoreCase(check)){
// 验证码错误
ResultInfo info = new ResultInfo();
//注册失败
info.setFlag(false);
info.setErrorMsg("验证码错误");
//将info对象序列化为json
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(info);
//将json数据写回客户端
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
return;
}
//获取数据
Map<String, String[]> map = request.getParameterMap();
//封装对象
User user = new User();
BeanUtils.populate(user,map);
// 调用service完成注册
// UserServlceImpl userService = new UserServlceImpl();
boolean flag = servlce.regist(user);
ResultInfo info = new ResultInfo();
//响应结果
if (flag) {
//注册成功
info.setFlag(true);
} else {
//注册失败
info.setFlag(false);
info.setErrorMsg("注册失败");
}
//将info对象序列化为json
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(info);
//将json数据写回客户端
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
}
/**
* 登录功能
*/
public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取用户名称和密码
Map<String, String[]> map = req.getParameterMap();
// 封装User对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
// 调用Service查询
// UserServlce servlce = new UserServlceImpl();
User u= servlce.login(user);
ResultInfo info = new ResultInfo();
// 判断用户对象是后为null
if (u==null){
System.out.println("密码错误");
// 用户名和密码错误
info.setFlag(false);
info.setErrorMsg("用户密码错误");
}
// 判断用户是否激活
if (u!=null&&!"Y".equals(u.getStatus())){
// 用户尚未激活
info.setFlag(false);
info.setErrorMsg("您尚未激活,请激活..");
}
// 判断登录成功
if(u!=null&&"Y".equals(u.getStatus())){
req.getSession().setAttribute("user",u);
info.setFlag(true);
}
// 响应数据
ObjectMapper mapper = new ObjectMapper();
resp.setContentType("application/json;charset=utf-8");
mapper.writeValue(resp.getOutputStream(),info);
}
/**
*查单个对象
*/
public void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从session中获取登录用户
Object user = req.getSession().getAttribute("user");
// 将user写回客户端
ObjectMapper mapper = new ObjectMapper();
resp.setContentType("application/json;charset=utf-8");
mapper.writeValue(resp.getOutputStream(),user);
}
/**
*退出
*/
public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 销毁session
req.getSession().invalidate();
// 跳转页面
resp.sendRedirect(req.getContextPath()+"login.html");
}
/**
*激活功能
*/
public void activea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取激活码
String code = req.getParameter("code");
if (code!=null){
// 调用servlet完成激活
// UserServlce servlce = new UserServlceImpl();
boolean b = servlce.active(code);
// 判断标记
String msg = null;
if (b){
msg="激活成功,请<a href='login.html'>登录</a>";
}else{
msg="激活失败,请联系管理员";
}
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write(msg);
}
}
}
分类数据展示功能_分析
图解:
标签:info,抽取,UserServlet,resp,json,user,new,import,页面 From: https://www.cnblogs.com/ssr1/p/16633467.html