1、Request实现请求转发(与下面的Response实现重定向 区分)
请求转发资源共享数据:使用Request对象
void setAttribute(String name,Object o)存储数据到request域中
Object getAttribute(String name) 根据key 获取值
void removeAttribute(Sting name) 根据key,删除该键值对
案例 web-demo01
2、Response实现重定向
response.sendRedirect("/web-demo01/resp02");
案例 web-demo01
3、Cookie
4、Session
\
1 brand-demo的增 删 该都是用的request的
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
2 brand-demo查询的用的是 只是存上了数据
// 封装到request域中
request.setAttribute("brand",brand);
// 转发
request.getRequestDispatcher("update.jsp").forward(request,response);
3 brand-demo的登录页面用的是
登陆成功 Session资源共享
// session共享资源
HttpSession session = request.getSession();
session.setAttribute("user",user);
// 实现重定向
response.sendRedirect("/brand-demo/selectAllServlet");
我自己想的
// request.setAttribute("login_name", user.getUsername()); // request.getRequestDispatcher("/selectAllServlet").forward(request, response);
登陆失败 用的是
// 存储信息到request域
request.setAttribute("login_msg", "用户名或密码错误");
// 转发
request.getRequestDispatcher("/login.jsp").forward(request, response);
我自己想的
// HttpSession session = request.getSession(); // session.setAttribute("login_msg","用户名或密码错误"); // response.sendRedirect("/brand-demo/login.jsp");
4 brand-demo的登录页面验证码用的是Cookie
//用户勾选了记住用户
Cookie c_username = new Cookie("username", username);
Cookie c_password = new Cookie("password", password);
//记住时间
c_username.setMaxAge(60*60*24*7);
c_password.setMaxAge(60*60*24*7);
//发送Cookie
response.addCookie(c_username);
response.addCookie(c_password);