客户端转发
- 浏览器地址栏信息改变,不再是提交表单的路径
- 客户端转发就是发送了一次新的请求,在整个登录中发送了两次的请求(1.点击登录按钮提交表单时候的请求 2.在servlet中使用sendRediect方法实现跳转(跳转到欢迎界面))
- 客户端发转发之后在request内置对象中的属性就不存在了(因为是一次新的请求,而request只能表示当前请求)
如果让客户端跳转之后保存的属性还有效,则可以使用session内置对象。
服务器端转发
- 浏览器的地址栏信息没发生改变
- 转发欢迎页面(jsp页面)之后依然可以取得保存在request内置对象中的属性
- 其实对于客户端来说只发送了一次请求
package com.biubiu.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.biubiu.factory.ServiceFactory;
import com.biubiu.service.IEmpService;
import com.biubiu.service.impl.EmpServicelmpl;
import com.biubiu.vo.Emp;
public class EmpServlet extends HttpServlet{
//取得业务层实现类对象
private IEmpService empService = (IEmpService)ServiceFactory.getInstance(EmpServicelmpl.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
// HttpSession session = req.getSession();
// String id = session.getId();
// System.out.println(id);
String pathInfo = req.getPathInfo();
try {
if ("/login".equals(pathInfo)) {
this.login(req, resp);
}else if ("/remove".equals(pathInfo)) {
this.removeById(req, resp);
}else if("/logout".equals(pathInfo)){
this.logout(req, resp);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void logout(HttpServletRequest req, HttpServletResponse resp)throws Exception{
//销毁session对象
req.getSession().invalidate();
System.out.println("注销用户");
resp.sendRedirect("/MvcProject/Pages/login.jsp");
}
/**
* 删除数据
* @param req
* @param resp
* @throws Exception
*/
public void removeById(HttpServletRequest req, HttpServletResponse resp)throws Exception{
Integer empno = Integer.parseInt(req.getParameter("id"));
//登录验证
if (req.getSession().getAttribute("emp")==null) {
//表示没有登录
req.setAttribute("msg", "只有登录后才能取得数据");
req.getRequestDispatcher("/Pages/login.jsp").forward(req, resp);
}else {
System.out.println(empService.removeEmpById(empno));
}
}
public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception{
String name = req.getParameter("username");
String pwd = req.getParameter("pwd");
//查询数据库中的用户名和密码与用户输入进行对比
Emp emp = empService.findLogin(name,pwd);//服务层的方法
if (emp!=null) {
//将用户信息保存到session内置对象
req.getSession().setAttribute("emp", emp);//req.setAttribute("emp", emp)数据没有保存
resp.sendRedirect("/MvcProject/Pages/welcome.jsp");
}else {
//重新返回登录页面(使用服务端转发)
req.getRequestDispatcher("/login.html").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
this.doGet(req, resp);
}
}
session的工作机制
当第一次调用getSession就创建了session对象,并且为该对象分配一个id,将这个ID保存到session对象中,再复制一个副本一cookie的方式保存到浏览器。
在以后再次调用getSession的时候将客户端的cookie进行遍历判断。如果有JESSIONID能和服务端的id匹配则不再产生新的session内置对象,而是返回该对象
访问jsp页面的时候会产生session对象,JSP是一个特殊的servlet,默认调用了getSession方法。
当访问一个jsp页面的时候会将jsp转行成一个*.java原码文件,之后再将该类型转换成.class文件,其实转换后的.class文件就是一个特殊的servlet。而且在servlet中调用getSession。