request和response的知识

Request 和 Response 对象起到了服务器与客户机之间的信息传递作用。Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏览器。

一、Request对象的五个集合:

QueryString:用以获取客户端附在url地址后的查询字符串中的信息。

    例如:stra=Request.QueryString ["strUserld"]

Form:用以获取客户端在FORM表单中所输入的信息。(表单的method属性值需要为POST)

    例如:stra=Request.Form["strUserld"]

Cookies:用以获取客户端的Cookie信息。

    例如:stra=Request.Cookies["strUserld"]

ServerVariables:用以获取客户端发出的HTTP请求信息中的头信息及服务器端环境变量信息。

    例如:stra=Request.ServerVariables["REMOTE_ADDR"],返回客户端IP地址

ClientCertificate:用以获取客户端的身份验证信息

    例如:stra=Request.ClientCertificate["VALIDFORM"],对于要求安全验证的网站,返回有效起始日期。

二、Response对象

    Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应。Response对象提供了一个数据集合cookie,它用于在 客户端 写入cookie值。若指定的cookie不存在,则创建它。若存在,则将自动进行更新。结果返回给客户端浏览器。

    语法格式:Response.Cookies(CookieName)[(key)|.attribute]=value。这里的CookiesName是指定的Cookie的名称,如果指定了Key,则该Cookie就是一个字典,Attribute属性包括Domain,Expires,HasKeys,Path,Secure。

response的方法:

    Write:向客户端发送浏览器能够处理的各种数据,包括:html代码,脚本程序等。

    Redirect:response.redirect("url")的作用是在服务器端重定向于另一个网页。

    End:用来终止脚本程序

    Clear:要说到Clear方法,就必须提到response的Buffer属性,Buffer属性用来设置服务器端是否将页面先输出到缓冲区。语法为:Response.Buffer=True/False

    Flush:当Buffer的值为True时,Flush方法用于将缓冲区中的当前页面内容立刻输出到客户端。


public class Demo1 extends HttpServlet {


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

//在servlet中用outputstream输出中文的问题
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test4(response);
}
private void test4(HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
//如果servle的代码写成这样"response.getOutputStream().write(1);",因为
//浏览器默认的编码是gb2312,那么它会去寻找编号为1所对应的字符,结果是为"",
//如果写成字符串"1"的话,那么这个1字符是经过getBytes之后的,所以会直接输出1
response.getOutputStream().write(1);
response.getOutputStream().write("1".getBytes());
}
private void test3(HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
String name="中国3";
//如果程序把text/html后面的;写成了,的话,那么浏览器会提示下载此servlet文件
response.setHeader("Content-type", "text/html,charset=UTF-8");
response.getOutputStream().write(name.getBytes("UTF-8"));
}
private void test2(HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
String name="中国2";
//用html技术中的meta标签来模拟http的响应头,来控制浏览器的行为
response.getOutputStream().write("<meta http-equiv='content-type' content='text/type;charset=UTF-8'>".getBytes());
response.getOutputStream().write(name.getBytes("UTF-8"));
}
private void test1(HttpServletResponse response)
throws IOException, UnsupportedEncodingException {
String name="中国1";
//程序以什么码表输出了,程序就要控制浏览器以什么样的码表打开
response.setHeader("Content-type", "text/html;charset=UTF-8");
//response.getOutputStream().write(name.getBytes());
response.getOutputStream().write(name.getBytes("UTF-8"));
}
}

public class Demo2 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
//通过response的wirter流输出数据
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//因为在servlet传递给浏览器的过程中是通过response进行编码后传递的,而老外默认是
//使用iso8859-1来进行编码传递的,所以我们需要对response的编码进行设置,以控制
//response以什么码表向浏览器写出数据
//测试得知,在设置response的编码时最好写在上面

//第一种方式,控制response的编码和浏览器显示的编码,因为浏览器默认是gb2312的
//response.setCharacterEncoding("utf-8");
//下面这句通过response设置浏览器的编码,其实默认同时也把response的编码也给设置了,所以上面的那句话也可以省略掉了
//response.setContentType("text/html;charset=utf-8");

//第二种方式,控制response的编码与浏览器的一致,也就是gb2312编码
response.setCharacterEncoding("gb2312");

String name="中国";
PrintWriter out=response.getWriter();
//第三种方式,不设置response的编码,使用默认的iso8859-1,然后把string转化为8859-1后进行传递
//out.print(new String(name.getBytes(),"iso8859-1"));

out.print(name);

}
}

//文件下载
public class Demo3 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path=this.getServletContext().getRealPath("/download/小破孩.jpg");
String filename=path.substring(path.lastIndexOf("/")+1);
//如果下载文件是中文文件,那么文件名需要经过url编码
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"utf-8"));
InputStream is=new FileInputStream(path);
OutputStream os=response.getOutputStream();
int len=0;
byte[] bs=new byte[1024];
while((len=is.read(bs))>0){
os.write(bs, 0, len);
}
os.close();
is.close();
}
}

public class Demo5 extends HttpServlet {


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


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//test1(response);
//test2(response);
test3(request,response);
}
//实用的跳转技术,最终的信息还是要在浏览器中显示比较好,这样的话容易排版,test2中是输出的是直接页面的源代码
private void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String message="<meta http-equiv='refresh' content='3;url=/testweb/index.jsp'>恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,<a href=\"/index.jsp\">请点击此处</a>";
request.setAttribute("message", message);
this.getServletContext().getRequestDispatcher("/forword.jsp").forward(request, response);

}
private void test2(HttpServletResponse response) throws IOException {
//假设这是一个用于登录的Servlet
//假设程序运行到此,用户登录成功了
response.setContentType("text/html;charset=gb2312");
response.setHeader("refresh", "3;url='/testweb/index.jsp'");
response.getWriter().write("恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,<a href=\"/index.jsp\">请点击此处</a>");
}


private void test1(HttpServletResponse response) throws IOException {
response.setHeader("refresh", "3");//每隔三秒刷新一次
int data=new Random().nextInt(1000);
response.getWriter().println(data);
}


}

public class Demo5 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//使用expries缓存当前内容
response.setDateHeader("expires", System.currentTimeMillis()+1000*3600);
String data="bbbbbbbbbbbbbbbbbb";
System.out.println("访问---");
response.getWriter().write(data);
}


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


}

/**
 * 重定向的特点:
 * 1.浏览器会向服务器发送两次请求,意味着就有两个request/response
 * 2.用重定向技术,地址栏会发生变化
 *
 *用户登录和购物车时,通常会用到重定向技术
 *
 *同时调用getOutPutStream()和getWriter()会抛出异常
 */
public class Demo6 extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*sendRedirect的内部原理
response.setStatus(302);
response.setHeader("location", "/testweb/index.jsp");
*/
response.sendRedirect("/testweb/index.jsp");
}


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


}


getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 
getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值