Servlet中的Request的一些实现细节和应用

    Servlet中,response是向浏览器发出请求,而request就是获得浏览器向服务器发出的请求了,下面首先演示request的常用方法

package com.bird.request;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @use Request的常用方法
 * @author Bird 
 *
 */
public class RequestDemo1 extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		System.out.println("request" + request.getRequestURI());
//		System.out.println("request" + request.getRequestURL());
		
//		System.out.println(request.getQueryString());//得到查询窜
//		System.out.println(request.getRemoteAddr());//得到访问的IP地址
//		System.out.println(request.getRemotePort());//得到访问端口
		System.out.println(request.getMethod());//得到请求方式
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}


获取求情头的信息和数据

package com.bird.request;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @use 获取请求头信息和数据
 * @author Bird
 *
 */
public class RequestDemo2 extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		String username = request.getParameter("username");
//		System.out.println(username);
		
		
		
	}
	
	//获取头相关的方法
	private void test1(HttpServletRequest request) {
		String headValue = request.getHeader("Accept-Encoding");
		System.out.println(headValue);
		
		System.out.println("-----------------------------");
		
		Enumeration  en = request.getHeaders("Accept-Encoding");
		while(en.hasMoreElements()){
			String value = (String) en.nextElement();
			System.out.println(value);
		}
		
		
		System.out.println("++++++++++++++++++++++++++");
		
		en = request.getHeaderNames();
		while(en.hasMoreElements()){
			String value = (String) en.nextElement();
			System.out.println(value);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		System.out.println(username);
	}

}

控制浏览器的定时刷新

package com.bird.ServletResponse;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo3 extends HttpServlet {

	/**
	 * @use 控制浏览器定时刷新
	 * @author Bird
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//实用的自动跳转技术
		String message = "<meta http-equiv='refresh' content='3;url=/ServletDemo/index.jsp'>恭喜你登录成功,本浏览器将在3s后跳转,如果没有跳转,请点击<a href =''>连接</a>";
		this.getServletContext().setAttribute("message", message);
		this.getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
		
	}
	
	//控制登录完毕后几秒内跳转
	private void test2(HttpServletResponse response) throws IOException {
		//假设这个是一个处理登录的Servlet,用户到此就算是登录成功了
		//浏览器需要在登录成功后自动跳转
		response.setCharacterEncoding("UTF-8");//设置当前码表
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("refresh", "3;url='/ServletDemo/index.jsp'");
		response.getWriter().write("恭喜你登录成功,本浏览器将在3s后跳转,如果没有跳转,请点击<a href =''>连接</a>");
	}
	
	//控制浏览器定时刷新
	private void test1(HttpServletResponse response) throws IOException {
		response.setHeader("refresh", "3");//控制浏览器每隔3秒访问一次服务器也就是刷新
		String date = new Date().toString();
		response.getOutputStream().write(date.getBytes());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}



Expries头控制缓存

package com.bird.ServletResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo4 extends HttpServlet {

	/**
	 * @use 使用Expries头控制缓存
	 * @author Bird
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//一定要弄当前时间加上你所需要的时间才行
		response.setDateHeader("expires", System.currentTimeMillis()+1000*3600);
		String date = "aaaaaaa";
		response.getWriter().write(date);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}


重定向的特点

package com.bird.ServletResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 重定向的特点
 * 1.浏览器会向服务器发送两次,意味着有两个response
 * 2.用重定向,浏览器地址栏会发生变化
 * 3.必须使用购物车才可以
 * 4.同时调用getoutputstream和getwriter会抛出这个已经使用的异常,特别是重定向的时候
 */
public class ResponseDemo5 extends HttpServlet {

	/**
	 * @use 使用response实现请求的重定向
	 */
	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		response.setStatus(302);//设置重定向状态码
//		response.setHeader("location", "/ServletDemo/index.jsp");
		
		response.sendRedirect("/ServletDemo/index.jsp");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}


你可能感兴趣的:(Servlet中的Request的一些实现细节和应用)