[java] view plain copy
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"));  
}  

}
[java] view plain copy
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);  
      
}  

}

[java] view plain copy
//文件下载
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();  
}  

}

[java] view plain copy
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);  
}  

}

[java] view plain copy
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);  
}  

}

[java] view plain copy
/**

  • 重定向的特点:
  • 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);  
}  

}

posted on 2016-07-28 10:47  cyy_13  阅读(217)  评论(0编辑  收藏  举报