美文网首页
java web 学习

java web 学习

作者: 小菜_charry | 来源:发表于2016-06-09 22:57 被阅读92次

servlet 的理解

servlet:java server applet 服务端小程序,它是JSP组件的前身,是java web开发技术的基础和核心组件。Servlet对象给web应用进行了封装,针对HTTP请求提供了丰富的API方法。

1、当服务器接受到请求,会创建request和response对象。
2、会将请求消息数据封装在request对象中。
3、将这两个对象穿阿迪给service方法作为参数与
4、当服务器给浏览器作出响应之前,会去response对象中获取设置的响应消息数据,然后作出真正响应

service(request,response);
1、通过request获取请求消息数据
2、通过response设置响应消息

1.接受请求,服务器创建request和response对象
2.将请求消息数据封装到request对象中
3.将这两个对象传递给service方法,调用service方法
4.在service方法中,我们可以通过request对象来获取请求消息数据,通过response对象类设置响应消息数据
5.在服务器真正作出响应之前,会从response对象中获取出我们设置的响应消息数据。
6.当响应完成,这两个对象将被销毁。

request的全路径:org.apache.catalina.connector.Request
response的全路径:org.apache.catalina.connector.Response



响应头及重定向的理解:

  • 设置响应状态码: setStatus(int sc):
  • 设置响应头:有三种响应头设置方式
    • setHeader(String name,String value):
    • setIntHeader(String name, int value)
    • setDateHeader(String name, long date)
  • content-type:告诉浏览器,当前发送的响应数据的mime类型 以及编码方式。浏览器就会使用相应的解析引擎来解析数据
    • response.setContentType("text/html;charset=utf-8");解决中文乱码问题

而重定向就是设置响应状态码为302,设置响应头的key为"location"值设置为跳转的地址(sendRedirect将其封装好,直接使用sendRedirect即可)。

/**
 * 完成重定向
 * @author super
 *
 */
public class ResponseDemo1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    /*//1.设置响应状态码  302
    response.setStatus(302);
    //2.设置响应头
    response.setHeader("location", "http://localhost:8080/day08/req");
    */
    //简化版重定向
    response.sendRedirect("/day08/regist.html");

}

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

    this.doGet(request, response);
}

}
  • 重定向特点:
    1.地址栏会发生变化
    2.可以访问外部的站点
    3.两次请求

response输出:

  • 输出一张图片
public class ResponseDemo3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //1.获取字节输出流
    ServletOutputStream sos = response.getOutputStream();
    //2.定义输入流关联图片
    String realPath = this.getServletContext().getRealPath("/404.jpg");//获取图片真实路径
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));
    //3.流的对拷
    IOUtils.copy(bis, sos);
    
    bis.close();
    
    
}

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

    this.doGet(request, response);
}
}
  • 向浏览器设置验证码
public class CheckCodeServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //1.在内存中创建一张图片
    int width = 100;
    int height = 50;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    //设置背景色
    Graphics g = image.getGraphics();//获取画笔对象
    g.setColor(Color.PINK);
    g.fillRect(0, 0, width, height);
    
    //画边框
    g.setColor(Color.BLUE);
    g.drawRect(0, 0, width - 1, height - 1);
    
    //写入验证码
    g.setColor(Color.BLACK);
    /*g.drawString("A", 20, 25);
    g.drawString("B", 40, 25);
    g.drawString("c", 60, 25);
    g.drawString("d", 80, 25);*/
    String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    Random ran = new Random();
    for (int i = 1; i <= 4; i++) {
        int index = ran.nextInt(str.length());
        g.drawString(String.valueOf(str.charAt(index)), width/5*i, height/2);
    }
    
    //画干扰线
    g.setColor(Color.GREEN);
    
    for (int i = 0; i < 10; i++) {
        int x1 = ran.nextInt(width);
        int x2 = ran.nextInt(width);
        int y1 = ran.nextInt(height);
        int y2 = ran.nextInt(height);
        g.drawLine(x1, y1, x2, y2);
    }
    
    
    //2.将图片输出到页面上去
    ImageIO.write(image, "jpg", response.getOutputStream());
}

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

    this.doGet(request, response);
    }
 }

request:请求对象

  • 获取请求消息
    • 获取请求行:GET /day08/req1 HTTP/1.1
      • getMethod():获取请求方式

      • getContextPath():获取虚拟目录(项目的访问方式)

      • getRequestURI():获取访问的URI /day08/req1

      • getRequestURL():获取访问的URL http://localhost:8080/day08/req1

      • getQueryString():get方式获取请求参数

      • getRemoteAddr():获取客户端的IP地址

在jsp中获取虚拟目录(项目的访问方式)

<body>
    
    ${pageContext.request.contextPath }
    <img src="${pageContext.request.contextPath }/checkCode">
    
</body>

请求头

  • 获取请求头:
    getHeader():获取请求头
    user-agent:获取浏览器的版本信息
    下面Chrome浏览器的user-agent的信息

      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
    

请求体:

  • 获取请求体:封装的是post请求的请求参数

    • BufferedReader getReader():获取字符体
    • ServletInputStream getInputStream();获取字节体,用于文件上传(获取文件上传表单数据)
  • 通用的获取请求参数的方式:
    String getParameter(String name):通过请求参数名称获取请求参数值。
    String[] getParameterValues(String name):通过请求参数名称获取请求参数值的数组。一般用于复选框
    Map<String,String[]> getParameterMap():获取所有参数的键值对

    Enumeration<E> getParameterNames():获取所有的名称的枚举

中文乱码问题

  • 解决请求参数中文乱码问题
    • 通用方式
      username = new String(username.getBytes("iso-8859-1"),"utf-8");

    • post方式:
      request.setCharacterEncoding("utf-8");

    //post解决中文乱码问题
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");

重定向和转发的区别与联系

  • 请求转发:

    • 一次请求,一次响应
    • 只能转发到内部站点
    • 地址栏不会发生变化
  • 重定向:

    • 两次请求,两次响应
    • 发到外部站点
    • 地址栏会发生变化

如果想在两个servlet之间共享数据,就需要用到转发

  • 域对象:代表一次请求
    • getAttribute()
    • setAttribute():
      • removeAttribute():

相关文章

网友评论

      本文标题:java web 学习

      本文链接:https://www.haomeiwen.com/subject/zrhsdttx.html