request内置对象(JSP九大内置对象之一)简述:
内置对象即已在容器内部创建完成,可以直接调用的对象。容器在接收到客户端的请求后会创建一个对象用于处理请求信息,该对象就是内置对象(属于“javax.servlet.http.HttpServletRequest”包下名称就是request,在调用service方法时容器会自行传递对应方法)总结:
1、基路径使用<base href="/JavaWebDemo01/">2、获取客户端参数3、将数据以键值对的方式保存在request内置对象中4、通过获取额外路径区分调用对应方法5、在serlvet中进行页面跳转public class ServletDemo01 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //获取额外路径 String pathInfo = req.getPathInfo(); System.out.println(pathInfo); //判断额外路径为/login调用登陆方法 if ("/login".equals(pathInfo)) { login(req, resp); } //判断额外路径为/logout调用注销方法 else if ("/logout".equals(pathInfo)) { logout(req, resp); } } //登陆方法 public void login(HttpServletRequest req, HttpServletResponse resp) { //登录的方法的账户信息比对 if ("xyz".equals(req.getParameter("username")) && "1234".equals(req.getParameter("password"))) { System.out.println("登录成功"); try { //通过response内置方法实现客户端跳转 resp.sendRedirect("/JavaWebDemo01/pages/TestDemo01.html"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { System.out.println("登录失败"); try { //通过request内置方法实现服务器端跳转 req.getRequestDispatcher("/pages/TestDemo02.html").forward(req, resp); } catch (ServletException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //注销方法 public void logout(HttpServletRequest req, HttpServletResponse resp) { System.out.println("注销成功"); try { //通过response内置方法实现客户端跳转 resp.sendRedirect("/JavaWebDemo01/pages/TestDemo02.html"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}