HttpServletRequest和HttpServletResponse实例

先看一下web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet>
<servlet-name>RequestDemo</servlet-name>
<servlet-class>cn.test.request.RequestDemo</servlet-class>
</servlet>
<servlet>
<servlet-name>ResponseDemo</servlet-name>
<servlet-class>cn.test.response.ResponseDemo</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>RequestDemo</servlet-name>
<url-pattern>/RequestDemo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResponseDemo</servlet-name>
<url-pattern>/ResponseDemo</url-pattern>
</servlet-mapping> </web-app>

当一个请求发送到tomcat服务器时,服务器会查询web.xml文件,先访问所有的<servlet-mapping>,匹配其中<url-pattern>,找到相应的信息后,提取<servlet-name>,然后分别匹配<servlet>中的<servlet-name>,当匹配到结果时,再根据相对应的<servlet-class>找到后台程序并执行,这就完成了一次访问过程。

requestdemo

 public class RequestDemo extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 设置参数查询的编码
* 该方法只能对请求实体内容的数据编码起作用。POST提交的数据在实体内容中,所以该方法对POST方法有效!
* GET方法的参数放在URI后面,所以对GET方式无效!!!
*/
request.setCharacterEncoding("utf-8"); /* System.out.println("GET方式");
//接收GET方式提交的参数
String value = request.getQueryString();
System.out.println(value);*/ /**
* 统一方便地获取请求参数的方法
*/
System.out.println(request.getMethod()+"方式");
//getParameter(name): 根据参数名得到参数值(只能获取一个值的参数)
String name = request.getParameter("name"); /**
* 手动重新解码(iso-8859-1 字符串-> utf-8 字符串)
*/
/*if("GET".equals(request.getMethod())){
name = new String(name.getBytes("iso-8859-1"),"utf-8");
}*/ String password = request.getParameter("password"); /*if("GET".equals(request.getMethod())){
password = new String(password.getBytes("iso-8859-1"),"utf-8");
}*/ System.out.println(name+"="+password); System.out.println("=============================");
Enumeration<String> enums = request.getParameterNames();
while( enums.hasMoreElements() ){
String paramName = enums.nextElement(); //如果参数名是hobit,则调用getParameterValues 获取多个参数值
if("hobit".equals(paramName)){
/**
* getParameterValues(name): 根据参数名获取参数值(可以获取多个值的同名参数)
*/
System.out.println(paramName+":");
String[] hobits = request.getParameterValues("hobit");
for(String h: hobits){
/* if("GET".equals(request.getMethod())){
h = new String(h.getBytes("iso-8859-1"),"utf-8");
}*/
System.out.print(h+",");
}
System.out.println();
//如果不是hobit,则调用getParameter
}else{
String paramValue = request.getParameter(paramName);
/*
if("GET".equals(request.getMethod())){
paramValue = new String(paramValue.getBytes("iso-8859-1"),"utf-8");
}*/ System.out.println(paramName+"="+paramValue);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*System.out.println("POST方式");
InputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while( (len=in.read(buf))!=-1 ){
System.out.println(new String(buf,0,len));
}*/ /**
* 统一方便地获取请求参数的方法
*/
/*System.out.println("POST方式");
//根据参数名得到参数值
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name+"="+password); System.out.println("=============================");
Enumeration<String> enums = request.getParameterNames();
while( enums.hasMoreElements() ){
String paramName = enums.nextElement();
String paramValue = request.getParameter(paramName);
System.out.println(paramName+"="+paramValue);
}*/ //一定调用doGet方式
this.doGet(request, response);
} }

responsedemo

public class ResponseDemo extends HttpServlet {

    /**
* 1)tomcat服务器把请求信息封装到HttpServletRequest对象,且把响应信息封装到HttpServletResponse
* 2)tomcat服务器调用doGet方法,传入request,和response对象
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 3)通过response对象改变响应信息
*/
/**
* 3.1 响应行
*/
//response.setStatus(404);//修改状态码
//response.sendError(404); // 发送404的状态码+404的错误页面 /**
* 3.2 响应头
*/
response.setHeader("server", "JBoss"); /**
* 3.3 实体内容(浏览器直接能够看到的内容就是实体内容)
*/
// response.getWriter().write("01.hello world"); //字符内容。
response.getOutputStream().write("02.hello world".getBytes());//字节内容 } /**
* 4)tomcat服务器把response对象的内容转换成响应格式内容,再发送给浏览器解析。
*/ }

response重定向:

 public class ResponseDemo2 extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 需求: 跳转到adv.html
* 使用请求重定向: 发送一个302状态码+location的响应头
*/
/*response.setStatus(302);//发送一个302状态码
response.setHeader("location", "/day09/adv.html"); //location的响应头
*/ //请求重定向简化写法
response.sendRedirect("/day09/adv.html"); } }

定时刷新:

 public class ResponseDemo extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 定时刷新
* 原理:浏览器认识refresh头,得到refresh头之后重新请求当前资源
*/
//response.setHeader("refresh", "1"); //每隔1秒刷新次页面 /**
* 隔n秒之后跳转另外的资源
*/
response.setHeader("refresh", "3;url=/day09/adv.html");//隔3秒之后跳转到adv.html
} }
上一篇:组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)


下一篇:[转载]C++ 堆与栈简单的介绍