request对象和response对象(5)
来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:18 点击:次
p> }
p>
p> //刷到别的页面
p> private void test2(HttpServletResponse response) throws IOException{
p> response.setContentType("text/html;charset=UTF-8");
p> PrintWriter out = response.getWriter();
p> response.setHeader("Refresh", "2;URL=/Response/index.jsp");
p> out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href='/Response/index.jsp'>这里</a>");
p> }
p>
p> public void doPost(HttpServletRequest request, HttpServletResponse response)
p> throws ServletException, IOException {
p> doGet(request,response);
p> }
p>}
p>复制代码
p>5.控制缓存时间
p>
p>复制代码
p>package cn.lsl.response;
p>
p>
p>
p>import java.io.IOException;
p>
p>import javax.servlet.ServletException;
p>
p>import javax.servlet.http.HttpServlet;
p>
p>import javax.servlet.http.HttpServletRequest;
p>
p>import javax.servlet.http.HttpServletResponse;
p>
p>
p>
p>public class ResponseDemo5 extends HttpServlet {
p>
p>
p>
p> public void doGet(HttpServletRequest request, HttpServletResponse response)
p>
p> throws ServletException, IOException {
p>
p> //缓存一小时
p>
p> response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000);
p>
p> response.getWriter().write("hello");
p>
p> }
p>
p>
p>
p> public void doPost(HttpServletRequest request, HttpServletResponse response)
p>
p> throws ServletException, IOException {
p>
p> doGet(request,response);
p>
p> }
p>
p>}
p>复制代码
p>6.请求重定向
p>
p>请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之 为请求重定向。
p>
p>地址栏会连,并发送2次请求,增加服务器负担。
p>
p>实现方式:response.sendRedirect();
p>
p>原理:302/307状态码和location头即可实现重定向
p>
p>复制代码
p>package cn.lsl.response;
p>import java.io.IOException;
p>import javax.servlet.ServletException;
p>import javax.servlet.http.HttpServlet;
p>import javax.servlet.http.HttpServletRequest;
p>import javax.servlet.http.HttpServletResponse;
p>
p>public class ResponseDemo6 extends HttpServlet {
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>