-
session
//a页面 <% request.getSession().setAttribute("key","session");%> <%=session.getId() %> <a href="requestPage.jsp">点击</a>
//跳转页面<%=session.getAttribute("key")%> <%=session.getId() %>
getSession() //创建session对象,用requst对象调用,参数:true:自动创建,false:不创建 session.getId() //获取session的id session.setMaxInactiveInterval(1*60*60) //设置session自动销毁时间,以秒为单位,这里是1小时 session.setAttribute("session", "12345678"); //往session域存储数据,object类型 session.getAttribute("session"); //取数据,取出是object类型 session.removeAttribute(“session”); //删除session域中指定的数据
2. request
通过该对象获取用户提交的信息 request.getParameter();
该对象包含了当前用户请求的上下文,包括表单信息,传递的参数信息等
还可以通过次对象获取session对象
HttpSession session=request.getSession();
本页面即可获得数据:
<form > 姓名:<input type="text" name="name" length="20"> 账号:<input type="text" name="id" length="20"> <input type="submit" value="go"><br> </form> <%=request.getParameter("name") %><br> <%=request.getParameter("id") %><br> <%! String a,b; %> <% if (request.getParameter("name")==null){ a=request.getParameter("name"); } if (request.getParameter("id")==null){ b=request.getParameter("name"); } %>
String username=request.getParameter("username"); //解决get请求中文乱码问题 username=new String(username.getBytes("iso-8859-1"),"utf-8");
不同页面跳转,使用action,servlet
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class s1 */ @WebServlet("/s1") public class s1 extends HttpServlet { private static final long serialVersionUID = 1L; public s1() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("hello"); request.setAttribute("name","Feathers"); request.getRequestDispatcher("requestPage.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <%@page import="java.util.*" %> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="s1"> 姓名:<input type="text" name="name" length="20"> 账号:<input type="text" name="id" length="20"> <input type="submit" value="go"><br> </form > <%=request.getParameter("name") %><br> <%=request.getParameter("id") %><br> <%! String a,b; %> <% if (request.getParameter("name")==null){ a=request.getParameter("name"); } if (request.getParameter("id")==null){ b=request.getParameter("name"); } // 向request域中存入一个键值对 %> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> hey@ <%System.out.println((String)request.getAttribute("name")); %> </body> </html>
request.getRequestDispatcher():(带数据)
request.getRequestDispatcher("想跳转的servlet名").forward(request, response);
request.getRequestDispatcher("文件路径").forward(request, response);
3.Response
该对象常常被用来完成页面的跳转、页面的自动刷新、页面的自动跳转、禁用缓存等功能
页面跳转的方法:
response.sendRedirect("页面的路径");
页面自动刷新的方法:
response.setHeader("refresh",3);每隔3秒刷新一次
页面自动跳转的方法:
response.setHeader("refresh","3;URL=commom.jsp?ref=aaa");
指定3秒后自动跳转到common.jsp页面并传递了值为aaa的参数ref
禁用缓存的方法:
response.setHeader("Cache=Control","no-cache");
response.serHeader("Pragma","no-cache");
response.setHeader("Expires",0);
禁用缓存后,当我们单击后退时,客户端会自动想服务器请求上一次页面