网页--->controller层------>service层--------->数据库
controller层:用于用户从网页传输,在controller层接受
service层:验证用户信息,并且操作数据库(多用于数据验证)
//设置请求和响应的编码方式,解决中文乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
获取网页中传过来的内容
getParameter();
String username=request.getParameter("username");
获取上下文路径(现在的网页地址)##
request.getContextPath()
重定向,不能携带任何信息
response.sendRedirect
response.sendRedirect(request.getContextPath()+"/user/index.html");
转发,可以携带信息
request.getRequestDispatcher("/login.html").forward()
request.getRequestDispatcher("/login.html").forward(request, response);
例子
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应的编码方式,解决中文乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
IUserDao dao=new UserDaoImpl();
List<TabUser> userList=dao.getUserByName(username);
String msg ="";//用于存放错误信息
if(userList!=null&&userList.size()>0){
//有密码
TabUser passuser=dao.getUserByNameAndPwd(username,password);
if(passuser!=null){
// msg="登录成功";
//判断是普通用户还是管理员用户,跳转到新页,
//重定向,不能携带任何信息
if("0".equals(passuser.getRole())){
response.sendRedirect(request.getContextPath()+"/user/index.html");
}else{
response.sendRedirect(request.getContextPath()+"/admin/index.html");
}
}else{
msg="密码错误";
//返回到登录页,并携带错误信息
//转发,可以携带信息
request.getRequestDispatcher("/login.html").forward(request, response);
}
}else{
msg= "用户名错误";
//返回到登录页,并携带错误信息
//转发,可以携带信息
request.getRequestDispatcher("/login.html").forward(request, response);
}
// response.getWriter().append(msg);
}