package info.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import info.model.UserInfoModel;
import info.model.UserModel;
import info.service.UserService;
import info.service.impl.UserServiceImpl;
import net.sf.json.JSONObject;
public class UserController extends HttpServlet {
private UserService us = new UserServiceImpl();
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String status = req.getParameter("status");
if(status.equals("add")) {
this.add(req,resp);
}else if(status.equals("login")) {
this.login(req,resp);
}else if(status.equals("exit")) {
this.exit(req,resp);
}else if(status.equals("regist")) {
this.regist(req,resp);
}else if(status.equals("pass")) {
this.pass(req,resp);
}else if(status.equals("queryOne")) {
this.queryOne(req,resp);
}
}
private void queryOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
String u_name = req.getParameter("username");
System.out.println(u_name);
List<UserInfoModel> list = us.queryOneX(u_name);
System.out.println(list);
if(list!=null){
req.setAttribute("u_name", u_name);
req.setAttribute("list", list);
req.getRequestDispatcher("/jsp/userIndex/userFull.jsp").forward(req, resp);
}else{
resp.sendRedirect(req.getContextPath()+"/jsp/userIndex/user.jsp");
}
}
private void pass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String u_name = req.getParameter("userName");
String u_password = req.getParameter("password");
UserModel userm = new UserModel(0, u_name, u_password, "", "");
UserModel um = us.queryOne(userm);
//如果返回值(um)为null,则表示登录失败,否则为登录成功。
if( um != null) {
//登录成功,将用户信息存放到session中,并跳转请求(首页)
String password = req.getParameter("newpassword");
int uim = us.updatePS(password, u_name);
if(uim!=0){
JSONObject json = new JSONObject();
json.put("num", uim);
PrintWriter out = resp.getWriter();
out.print(json);
}else{
System.out.println("更改失败!");
}
}
}
private void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
String u_name = req.getParameter("userName");
String u_password = req.getParameter("password");
String u_nicheng = req.getParameter("nicheng");
UserModel um = us.insert(u_name,u_password,u_nicheng);
if(um!=null){
System.out.println("注册成功");
resp.sendRedirect(req.getContextPath()+"/jsp/login.jsp");
}else{
System.out.println("注册失败!");
}
}
private void exit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// TODO Auto-generated method stub
req.getSession().invalidate();
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}
private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
String userName = req.getParameter("userName");
String password = req.getParameter("password");
UserModel um = us.login(userName, password);
//如果返回值(um)为null,则表示登录失败,否则为登录成功。
if( um != null) {
//登录成功,将用户信息存放到session中,并跳转请求(首页)
req.getSession().setAttribute("userModel", um);
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}else {
//登录失败。返回登录页面,并提示。
req.setAttribute("msg", "用户名密码错误,请重新登陆。");
req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
String ui_name = req.getParameter("name");
String ui_sex = req.getParameter("sex");
String ui_birthday = req.getParameter("birthday");
String ui_idCard = req.getParameter("idCard");
int idCard = 0;
try {
idCard = Integer.parseInt(ui_idCard);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
String ui_tel = req.getParameter("tel");
int tel = 0;
try {
tel = Integer.parseInt(ui_tel);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
String ui_qq = req.getParameter("qq");
int qq = 0;
try {
qq = Integer.parseInt(ui_qq);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
List<UserInfoModel> list = us.insert(0,ui_name, ui_sex,tel, ui_birthday, idCard, qq);
if(list!=null){
System.out.println("新增成功");
resp.sendRedirect(req.getContextPath()+"/jsp/userIndex/userIndex.jsp");
}else{
System.out.println("新增失败!");
}
}
}
评论0