request对象和response对象

北城以北 提交于 2020-01-16 13:43:38

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象

HttpServletResponse

HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。

1.向客户端输出中文数据(字节)

复制代码
复制代码
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test5(response);
    }
    
    //输出整数:最好转换成字符串输出
    private void test5(HttpServletResponse response) throws IOException{
        int i = 98;
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //以字节流用默认编码向客户端输出中文数据:没有乱码
    //默认的是GBK
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes());
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    //以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        //通知客户端查看UTF-8码表
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        ServletOutputStream out = response.getOutputStream();
        out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test4(HttpServletResponse response) throws IOException{
        String s = "你好!四";
        response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

分析:出现乱码的图解

 

解决办法:

1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)

2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码

3)通过meta标签模拟请求头:out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());

4)通过以下方法:response.setContentType("text/html;charset=UTF-8");

2.向客户端输出中文数据(字符)

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo2 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test3(response);
    }

    //字符流输出中文数据
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        response.setCharacterEncoding("UTF-8");    //设置查的UTF-8(对内容进行编码,查utf-8)
        response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
        PrintWriter out = response.getWriter();
        out.write(s);//默认查的是ISO-8859-1码表
    }
    //字符流输出中文数据
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }
    //字符流输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }

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

分析:

乱码原因:以默认编码发送数据 ISO-8859-1(没有中国二字编码),此时会发生乱码

解决办法:

1)

setCharacterEncoding(“UTF-8”);//更改编码为UTF-8

response.setHead(“Context-type”,”text/html;charset=UTF-8”);//告诉客户端编码方式

2)response.setContentType("text/html;charset=utf-8");

在字符流输出中文数据时:

response.setContentType("text/html;charset=utf-8");有两个作用:

通知字符流以UTF-8编码输出

通知客户端以UTF-8解码显示

3.输出随机验证码

复制代码
复制代码
package cn.lsl.response;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        
        int width = 120;
        int height = 25;
        //创建一副内存图像BufferedImage
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到属于该图片的画笔:Graphics();
        Graphics g = image.getGraphics();
        //画边框
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width, height);
        //填充背景色
        g.setColor(Color.YELLOW);
        g.fillRect(1, 1, width-2, height-2);
        //画干扰线
        g.setColor(Color.GRAY);
        Random r = new Random();
        for(int i=0; i<10; i++)
            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
        //随机数字
        g.setColor(Color.RED);
        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
        int x = 23;
        for(int i=0; i<4; i++){
            g.drawString(r.nextInt(10)+"", x, 20);
            x = x+20;
        }
        //输出到浏览器的页面上:ImageIO
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

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

4.定时刷新

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo4 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test2(response);
    }
    
    //定时刷新到自己
    private void test1(HttpServletResponse response) throws IOException{
        Random r = new Random();
        int i = r.nextInt();
        response.setIntHeader("Refresh", 1);
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //刷到别的页面
    private void test2(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setHeader("Refresh", "2;URL=/Response/index.jsp");
        out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href='/Response/index.jsp'>这里</a>");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

5.控制缓存时间

复制代码
复制代码
package cn.lsl.response;

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 ResponseDemo5 extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       //缓存一小时

       response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000);

       response.getWriter().write("hello");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       doGet(request,response);

    }

}
复制代码
复制代码

6.请求重定向

请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之         为请求重定向。

地址栏会连,并发送2次请求,增加服务器负担。

实现方式:response.sendRedirect();

原理:302/307状态码和location头即可实现重定向

复制代码
复制代码
package cn.lsl.response;
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 ResponseDemo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setStatus(302);
//        response.setHeader("Location", "/Response/index.jsp");
        response.sendRedirect("/Response/index.jsp");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

7.getOutputStream和getWriter方法分别用于得到输出二进制数据、输出文本数据的ServletOuputStream、Printwriter对象。

getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。会抛异常。

异常为:getOutputStream() has already been called for this response

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;

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

//字节流和字符流同时使用,报错
//getOutputStream() has already been called for this response
public class ResponseDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s1 = "a";
        response.getOutputStream().write(s1.getBytes());
        String s2 = "b";
        response.getWriter().write(s2);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:通过字符流或字节流输出的数据并不是直接打给浏览器的。而是把数据写到response对象的缓存中的。服务器从缓存中取出数据,按照HTTP协议的响应格式输出给浏览器。如果你调用的response的输出流没有主动关闭,服务器会替你关的。

 

HttpServletRequest

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象的方法,可以获得客户这些信息。

1.获取客户机信息的方法

getRequestURL方法返回客户端发出请求时的完整URL。

getRequestURI方法返回请求行中的资源名部分。

getQueryString 方法返回请求行中的参数部分。

getRemoteAddr方法返回发出请求的客户机的IP地址

getRemoteHost方法返回发出请求的客户机的完整主机名

getRemotePort方法返回客户机所使用的网络端口号

getLocalAddr方法返回WEB服务器的IP地址。

getLocalName方法返回WEB服务器的主机名

getMethod得到客户机请求方式

eg:

复制代码
复制代码
package cn.lsl.request;
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 RequestDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取URL和URI
        //url:http://localhost:8080/Request/servlet/RequestDemo1
        //uri:/Request/servlet/RequestDemo1
        System.out.println("url:"+request.getRequestURL());
        System.out.println("uri:"+request.getRequestURI());
        
        //通过getQueryString可以获得get方式提交查询串url中?后面部分
        System.out.println("queryString:"+request.getQueryString());
        //获得客户端ip
        System.out.println("ip:"+request.getRemoteAddr());
        //通过getContextPath获得工程虚拟目录名称
        System.out.println("工程虚拟目录名称:"+request.getContextPath());
        //通过getMethod获得请求方式
        System.out.println("请求方式:"+request.getMethod());
        //获得当前访问资源路径 ----/servlet/RequestDemo1
        System.out.println("当前访问资源路径:"+request.getRequestURI().substring(request.getContextPath().length()));
    }

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

注:

1)URL和URI的区别

URL: http://localhost:8080/Request/servlet/RequestDemo1

URI: /Request/servlet/RequestDemo1

URI 包含 URL的 ,URL 一定完成路径,URI可以相对路径

http://localhost:8080/Request/servlet/RequestDemo1

是一个URL 同时也是 URI

/Request/servlet/RequestDemo1 ---- 都是URI 不是URL

2)获得当前访问资源路径 :

request.getRequestURI().substring(request.getContextPath().length());

2.获取请求头信息

getHeader 获得头信息的值,转换一个字符串

getHeaders 获得头信息值 ,获得Enumeration

getHeaderNames 获得所有头信息名称  返回 Enumeration

eg:

复制代码
复制代码
package cn.lsl.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;

public class RequestDemo2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取指定头信息字段
        String value = request.getHeader("Accept-Encoding");
        System.out.println(value);
        System.out.println("---------------");
        //获得所有头信息内容
        Enumeration<String> enumeration = request.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String name = enumeration.nextElement();
            System.out.println(name+":"+request.getHeader(name));
        }
    }

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

}
复制代码
复制代码

3.防盗链程序

referer.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>referer.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>通过链接访问RequestDemo3 不是盗链</h1>
    <a href="/Request/servlet/RequestDemo3">link</a>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 RequestDemo3 extends HttpServlet {

    //防盗链
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通过getHeader获得referer头信息
        String referer = request.getHeader("referer");
        //判断头信息可以知道是不是盗链
        response.setContentType("text/html;charset=utf-8");
        if(referer == null || !referer.startsWith("http://localhost")){
            //盗链
            response.getWriter().println("是盗链");
        }else{
            //不是盗链
            response.getWriter().println("真不是盗链");
        }
    }

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

4.获取客户机请求参数

常用API四个

getParameter

getParameterValues

getParameterNames

getParameterMap

eg:

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>request.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>分别以get和post方式提交数据</h1>
    <h1>第一种:通过超链接提交数据以?方式</h1>
    <a href="/Request/servlet/RequestDemo4?name=zhangsan&city=shenzhen">超链接提交数据</a>
    <h1>第二种:通过form的post方式提交数据</h1>
    <form action="/Request/servlet/RequestDemo4" method="post">
        姓名<input type="text" name="name" /><br/>
        城市<input type="text" name="city" /><br/>
        <input type="submit" value="提交">
    </form>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDemo4 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println(name);
        String city = request.getParameter("city");
        System.out.println(city);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

5.获取复杂表单的数据及解决中文乱码问题

regist.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>regist.html</title>
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <form action="/Request/servlet/RequestDemo5" method="post">
            <table>
                <tr>
                    <td>
                        用户名
                    </td>
                    <td>
                        <input type="text" name="username" />
                    </td>
                </tr>
                <tr>
                    <td>
                        密码
                    </td>
                    <td>
                        <input type="password" name="password" />
                    </td>
                </tr>
                <tr>
                    <td>
                        性别
                    </td>
                    <td>
                        <input type="radio" name="gender" value="male" />
                        男
                        <input type="radio" name="gender" value="female" />
                        女
                    </td>
                </tr>
                <tr>
                    <td>
                        爱好
                    </td>
                    <td>
                        <input type="checkbox" name="hobby" value="sport" />
                        运动
                        <input type="checkbox" name="hobby" value="music" />
                        音乐
                        <input type="checkbox" name="hobby" value="game" />
                        游戏
                    </td>
                </tr>
                <tr>
                    <td>
                        城市
                    </td>
                    <td>
                        <select name="city">
                            <option value="beijing">
                                北京
                            </option>
                            <option value="shanghai">
                                上海
                            </option>
                            <option value="shenzhen">
                                深圳
                            </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        个人简介
                    </td>
                    <td>
                        <textarea rows="5" cols="60" name="introduce"></textarea>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="注册" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

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

public class RequestDemo5 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //解决post乱码
        request.setCharacterEncoding("utf-8");
        //通过getParameter获得指定数据
        String username = request.getParameter("username");
        System.out.println(username);
        
        //解决get乱码
//        username = URLEncoder.encode(username, "ISO-8859-1");    //用ISO-8859-1编码
//        username = URLDecoder.decode(username, "utf-8");    //用utf-8解码
//        username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
//        System.out.println(username);
        String gender = request.getParameter("gender");
        System.out.println(gender);
        
        //获取checkbox所有提交数据 --- getParameterValues
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));
        
        //打印所有请求提交的参数
        //方式一
        Enumeration<String> names = request.getParameterNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();    //获取每一个参数的名称
            System.out.println(name + ":" +Arrays.toString(request.getParameterValues(name)));
        }
        System.out.println("----------------");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> keys = parameterMap.keySet();
        for (String key : keys) {
            System.out.println(key + ":" + Arrays.toString(parameterMap.get(key)));
        }
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:解决中文乱码问题

//解决post乱码

request.setCharacterEncoding("utf-8");

//解决get乱码(手动编码)

username = URLEncoder.encode(username, "ISO-8859-1");   //用ISO-8859-1编码

username = URLDecoder.decode(username, "utf-8"); //用utf-8解码

或    

username = new String(username.getBytes("ISO-8859-1"),"UTF-8");

6.请求转发

1)request对象实现请求转发:请求转发指一个web资源收到客户端请求后,通知服务器调用另外一个web资源进行处理。

2)request对象提供了一个getRequestDispatcher方法,该方法返回一个RequestDispatcher对象,调用这个对象的forward方法可以实现请求转发。

3)request对象同时也是一个域对象,开发人员通过request对象在实现转发时,把数据通过request对象带给其它web资源处理。

eg:AServlet转发到BServlet

复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;

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

public class AServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通过request对象传递
        //向request域对象保存一个属性
        request.setAttribute("name", "lsl");
        //传递数据给BServlet
        RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet/BServlet");
        dispatcher.forward(request, response);
    }

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

}
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 BServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request.getAttribute("name"));
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:1、ServletContext对象的getRequestDispather(String path1)

    path1必须用绝对路径,即以”/”开头,若用相对路径会抛出异常     IllegalArgumentException

    2、ServletRequest对象的getRequestDispatcher(String path2)           path2可以用绝对路径也可以用相对路径

拓展:转发与重定向区别

1).转发一次请求、一次响应。重定向两次请求,两次响应

2).转发只能跳转到站内程序,重定向可定向任何站点

3).转发URL地址不变,重定向URL地址改变

4).转发对客户端不可见,重定向对客户度可见

5).转发共享一个Request中数据,重定向两次请求,不同Request对象,不能共享Request数据

7.包含

logo.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>logo.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>LOGO</h1>
  </body>
</html>
复制代码
复制代码

footer.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>footer.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>@COPY</h1>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 MainServlet1 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/logo.html").include(request, response);
        response.getOutputStream().write("<h1>Main 1</h1>".getBytes());
        request.getRequestDispatcher("/footer.html").include(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

import java.io.IO

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象

HttpServletResponse

HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。

1.向客户端输出中文数据(字节)

复制代码
复制代码
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test5(response);
    }
    
    //输出整数:最好转换成字符串输出
    private void test5(HttpServletResponse response) throws IOException{
        int i = 98;
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //以字节流用默认编码向客户端输出中文数据:没有乱码
    //默认的是GBK
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes());
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    //以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        //通知客户端查看UTF-8码表
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        ServletOutputStream out = response.getOutputStream();
        out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
        out.write(s.getBytes("UTF-8"));
    }
    
    //以字节流用utf-8编码向客户端输出中文数据
    private void test4(HttpServletResponse response) throws IOException{
        String s = "你好!四";
        response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(s.getBytes("UTF-8"));
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

分析:出现乱码的图解

 

解决办法:

1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)

2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码

3)通过meta标签模拟请求头:out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());

4)通过以下方法:response.setContentType("text/html;charset=UTF-8");

2.向客户端输出中文数据(字符)

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo2 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test3(response);
    }

    //字符流输出中文数据
    private void test1(HttpServletResponse response) throws IOException{
        String s = "你好!一";
        response.setCharacterEncoding("UTF-8");    //设置查的UTF-8(对内容进行编码,查utf-8)
        response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
        PrintWriter out = response.getWriter();
        out.write(s);//默认查的是ISO-8859-1码表
    }
    //字符流输出中文数据
    private void test2(HttpServletResponse response) throws IOException{
        String s = "你好!二";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }
    //字符流输出中文数据
    private void test3(HttpServletResponse response) throws IOException{
        String s = "你好!三";
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(s);
    }

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

分析:

乱码原因:以默认编码发送数据 ISO-8859-1(没有中国二字编码),此时会发生乱码

解决办法:

1)

setCharacterEncoding(“UTF-8”);//更改编码为UTF-8

response.setHead(“Context-type”,”text/html;charset=UTF-8”);//告诉客户端编码方式

2)response.setContentType("text/html;charset=utf-8");

在字符流输出中文数据时:

response.setContentType("text/html;charset=utf-8");有两个作用:

通知字符流以UTF-8编码输出

通知客户端以UTF-8解码显示

3.输出随机验证码

复制代码
复制代码
package cn.lsl.response;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        
        int width = 120;
        int height = 25;
        //创建一副内存图像BufferedImage
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //得到属于该图片的画笔:Graphics();
        Graphics g = image.getGraphics();
        //画边框
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width, height);
        //填充背景色
        g.setColor(Color.YELLOW);
        g.fillRect(1, 1, width-2, height-2);
        //画干扰线
        g.setColor(Color.GRAY);
        Random r = new Random();
        for(int i=0; i<10; i++)
            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
        //随机数字
        g.setColor(Color.RED);
        g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
        int x = 23;
        for(int i=0; i<4; i++){
            g.drawString(r.nextInt(10)+"", x, 20);
            x = x+20;
        }
        //输出到浏览器的页面上:ImageIO
        ImageIO.write(image, "jpg", response.getOutputStream());
    }

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

4.定时刷新

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo4 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test2(response);
    }
    
    //定时刷新到自己
    private void test1(HttpServletResponse response) throws IOException{
        Random r = new Random();
        int i = r.nextInt();
        response.setIntHeader("Refresh", 1);
        response.getOutputStream().write((i+"").getBytes());
    }
    
    //刷到别的页面
    private void test2(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setHeader("Refresh", "2;URL=/Response/index.jsp");
        out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href='/Response/index.jsp'>这里</a>");
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

5.控制缓存时间

复制代码
复制代码
package cn.lsl.response;

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 ResponseDemo5 extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       //缓存一小时

       response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000);

       response.getWriter().write("hello");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       doGet(request,response);

    }

}
复制代码
复制代码

6.请求重定向

请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之         为请求重定向。

地址栏会连,并发送2次请求,增加服务器负担。

实现方式:response.sendRedirect();

原理:302/307状态码和location头即可实现重定向

复制代码
复制代码
package cn.lsl.response;
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 ResponseDemo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        response.setStatus(302);
//        response.setHeader("Location", "/Response/index.jsp");
        response.sendRedirect("/Response/index.jsp");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

7.getOutputStream和getWriter方法分别用于得到输出二进制数据、输出文本数据的ServletOuputStream、Printwriter对象。

getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。会抛异常。

异常为:getOutputStream() has already been called for this response

复制代码
复制代码
package cn.lsl.response;

import java.io.IOException;
import java.io.PrintWriter;

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

//字节流和字符流同时使用,报错
//getOutputStream() has already been called for this response
public class ResponseDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s1 = "a";
        response.getOutputStream().write(s1.getBytes());
        String s2 = "b";
        response.getWriter().write(s2);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:通过字符流或字节流输出的数据并不是直接打给浏览器的。而是把数据写到response对象的缓存中的。服务器从缓存中取出数据,按照HTTP协议的响应格式输出给浏览器。如果你调用的response的输出流没有主动关闭,服务器会替你关的。

 

HttpServletRequest

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象的方法,可以获得客户这些信息。

1.获取客户机信息的方法

getRequestURL方法返回客户端发出请求时的完整URL。

getRequestURI方法返回请求行中的资源名部分。

getQueryString 方法返回请求行中的参数部分。

getRemoteAddr方法返回发出请求的客户机的IP地址

getRemoteHost方法返回发出请求的客户机的完整主机名

getRemotePort方法返回客户机所使用的网络端口号

getLocalAddr方法返回WEB服务器的IP地址。

getLocalName方法返回WEB服务器的主机名

getMethod得到客户机请求方式

eg:

复制代码
复制代码
package cn.lsl.request;
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 RequestDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取URL和URI
        //url:http://localhost:8080/Request/servlet/RequestDemo1
        //uri:/Request/servlet/RequestDemo1
        System.out.println("url:"+request.getRequestURL());
        System.out.println("uri:"+request.getRequestURI());
        
        //通过getQueryString可以获得get方式提交查询串url中?后面部分
        System.out.println("queryString:"+request.getQueryString());
        //获得客户端ip
        System.out.println("ip:"+request.getRemoteAddr());
        //通过getContextPath获得工程虚拟目录名称
        System.out.println("工程虚拟目录名称:"+request.getContextPath());
        //通过getMethod获得请求方式
        System.out.println("请求方式:"+request.getMethod());
        //获得当前访问资源路径 ----/servlet/RequestDemo1
        System.out.println("当前访问资源路径:"+request.getRequestURI().substring(request.getContextPath().length()));
    }

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

注:

1)URL和URI的区别

URL: http://localhost:8080/Request/servlet/RequestDemo1

URI: /Request/servlet/RequestDemo1

URI 包含 URL的 ,URL 一定完成路径,URI可以相对路径

http://localhost:8080/Request/servlet/RequestDemo1

是一个URL 同时也是 URI

/Request/servlet/RequestDemo1 ---- 都是URI 不是URL

2)获得当前访问资源路径 :

request.getRequestURI().substring(request.getContextPath().length());

2.获取请求头信息

getHeader 获得头信息的值,转换一个字符串

getHeaders 获得头信息值 ,获得Enumeration

getHeaderNames 获得所有头信息名称  返回 Enumeration

eg:

复制代码
复制代码
package cn.lsl.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;

public class RequestDemo2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取指定头信息字段
        String value = request.getHeader("Accept-Encoding");
        System.out.println(value);
        System.out.println("---------------");
        //获得所有头信息内容
        Enumeration<String> enumeration = request.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String name = enumeration.nextElement();
            System.out.println(name+":"+request.getHeader(name));
        }
    }

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

}
复制代码
复制代码

3.防盗链程序

referer.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>referer.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>通过链接访问RequestDemo3 不是盗链</h1>
    <a href="/Request/servlet/RequestDemo3">link</a>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 RequestDemo3 extends HttpServlet {

    //防盗链
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通过getHeader获得referer头信息
        String referer = request.getHeader("referer");
        //判断头信息可以知道是不是盗链
        response.setContentType("text/html;charset=utf-8");
        if(referer == null || !referer.startsWith("http://localhost")){
            //盗链
            response.getWriter().println("是盗链");
        }else{
            //不是盗链
            response.getWriter().println("真不是盗链");
        }
    }

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

4.获取客户机请求参数

常用API四个

getParameter

getParameterValues

getParameterNames

getParameterMap

eg:

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>request.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>分别以get和post方式提交数据</h1>
    <h1>第一种:通过超链接提交数据以?方式</h1>
    <a href="/Request/servlet/RequestDemo4?name=zhangsan&city=shenzhen">超链接提交数据</a>
    <h1>第二种:通过form的post方式提交数据</h1>
    <form action="/Request/servlet/RequestDemo4" method="post">
        姓名<input type="text" name="name" /><br/>
        城市<input type="text" name="city" /><br/>
        <input type="submit" value="提交">
    </form>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDemo4 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println(name);
        String city = request.getParameter("city");
        System.out.println(city);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码

5.获取复杂表单的数据及解决中文乱码问题

regist.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>regist.html</title>
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <form action="/Request/servlet/RequestDemo5" method="post">
            <table>
                <tr>
                    <td>
                        用户名
                    </td>
                    <td>
                        <input type="text" name="username" />
                    </td>
                </tr>
                <tr>
                    <td>
                        密码
                    </td>
                    <td>
                        <input type="password" name="password" />
                    </td>
                </tr>
                <tr>
                    <td>
                        性别
                    </td>
                    <td>
                        <input type="radio" name="gender" value="male" />
                        男
                        <input type="radio" name="gender" value="female" />
                        女
                    </td>
                </tr>
                <tr>
                    <td>
                        爱好
                    </td>
                    <td>
                        <input type="checkbox" name="hobby" value="sport" />
                        运动
                        <input type="checkbox" name="hobby" value="music" />
                        音乐
                        <input type="checkbox" name="hobby" value="game" />
                        游戏
                    </td>
                </tr>
                <tr>
                    <td>
                        城市
                    </td>
                    <td>
                        <select name="city">
                            <option value="beijing">
                                北京
                            </option>
                            <option value="shanghai">
                                上海
                            </option>
                            <option value="shenzhen">
                                深圳
                            </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        个人简介
                    </td>
                    <td>
                        <textarea rows="5" cols="60" name="introduce"></textarea>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="注册" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

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

public class RequestDemo5 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //解决post乱码
        request.setCharacterEncoding("utf-8");
        //通过getParameter获得指定数据
        String username = request.getParameter("username");
        System.out.println(username);
        
        //解决get乱码
//        username = URLEncoder.encode(username, "ISO-8859-1");    //用ISO-8859-1编码
//        username = URLDecoder.decode(username, "utf-8");    //用utf-8解码
//        username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
//        System.out.println(username);
        String gender = request.getParameter("gender");
        System.out.println(gender);
        
        //获取checkbox所有提交数据 --- getParameterValues
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));
        
        //打印所有请求提交的参数
        //方式一
        Enumeration<String> names = request.getParameterNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();    //获取每一个参数的名称
            System.out.println(name + ":" +Arrays.toString(request.getParameterValues(name)));
        }
        System.out.println("----------------");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> keys = parameterMap.keySet();
        for (String key : keys) {
            System.out.println(key + ":" + Arrays.toString(parameterMap.get(key)));
        }
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:解决中文乱码问题

//解决post乱码

request.setCharacterEncoding("utf-8");

//解决get乱码(手动编码)

username = URLEncoder.encode(username, "ISO-8859-1");   //用ISO-8859-1编码

username = URLDecoder.decode(username, "utf-8"); //用utf-8解码

或    

username = new String(username.getBytes("ISO-8859-1"),"UTF-8");

6.请求转发

1)request对象实现请求转发:请求转发指一个web资源收到客户端请求后,通知服务器调用另外一个web资源进行处理。

2)request对象提供了一个getRequestDispatcher方法,该方法返回一个RequestDispatcher对象,调用这个对象的forward方法可以实现请求转发。

3)request对象同时也是一个域对象,开发人员通过request对象在实现转发时,把数据通过request对象带给其它web资源处理。

eg:AServlet转发到BServlet

复制代码
复制代码
package cn.lsl.request;

import java.io.IOException;

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

public class AServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通过request对象传递
        //向request域对象保存一个属性
        request.setAttribute("name", "lsl");
        //传递数据给BServlet
        RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet/BServlet");
        dispatcher.forward(request, response);
    }

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

}
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 BServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request.getAttribute("name"));
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码

注:1、ServletContext对象的getRequestDispather(String path1)

    path1必须用绝对路径,即以”/”开头,若用相对路径会抛出异常     IllegalArgumentException

    2、ServletRequest对象的getRequestDispatcher(String path2)           path2可以用绝对路径也可以用相对路径

拓展:转发与重定向区别

1).转发一次请求、一次响应。重定向两次请求,两次响应

2).转发只能跳转到站内程序,重定向可定向任何站点

3).转发URL地址不变,重定向URL地址改变

4).转发对客户端不可见,重定向对客户度可见

5).转发共享一个Request中数据,重定向两次请求,不同Request对象,不能共享Request数据

7.包含

logo.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>logo.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>LOGO</h1>
  </body>
</html>
复制代码
复制代码

footer.html

复制代码
复制代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>footer.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
    <h1>@COPY</h1>
  </body>
</html>
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 MainServlet1 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/logo.html").include(request, response);
        response.getOutputStream().write("<h1>Main 1</h1>".getBytes());
        request.getRequestDispatcher("/footer.html").include(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }

}
复制代码
复制代码
复制代码
复制代码
package cn.lsl.request;

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 MainServlet2 extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/logo.html").include(request, response);
        response.getOutputStream().write("<h1>Main 2</h1>".getBytes());
        request.getRequestDispatcher("/footer.html").include(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
复制代码
复制代码
Exception; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MainServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/logo.html").include(request, response); response.getOutputStream().write("<h1>Main 2</h1>".getBytes()); request.getRequestDispatcher("/footer.html").include(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
复制代码
复制代码
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!