java web笔记之认识request和response

来源:互联网 发布:php 七牛上传图片 编辑:程序博客网 时间:2024/05/18 19:42

1.认识request

客户端浏览器发出的一个请求被封装成一个HttpServletRequest对象。所有的信息包括请求的地址,请求的参数,提交的数据,上传的文件,客户端的IP地址甚至客户端操作系统都包含在HttpServletRequest对象中。

2.认识response

服务器对客户端浏览器的响应被封装成一个HttpServletResponse对象。要对浏览器进行操作,只需要操作HttpServletResponse对象就可以了。通过HttpServletResponse.getWriter()获得一个PrintWriter对象,该对象为OutputStream的子类。然后使用该对象输出信息就可以了。
通过HttpServletResponse获取的PrintWriter对象只能写字符型的数据。如果需要在客户端写二进制数据,可以使用HttpServletResponse.getOutputStream()。方法getWriter()可以看做是方法getOUtoutStream()的一个封装。

3.response生成图片验证码

IdentityServlet
代码下载地址:http://download.csdn.net/detail/wangxuewei111/8493121
package com.wang.indentity;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class IdentityServlet extends HttpServlet{//随机字符串,不包括0O1I等难以辨认的字符串public static final char[] CHARS = {'2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'};public static Random random = new Random(); //随机数//获取6为随机数public static String getRandomString(){StringBuffer buffer = new StringBuffer();for (int i = 0; i < 6; i++) {buffer.append(CHARS[random.nextInt(CHARS.length)]);  //每次区一个随机字符串}return buffer.toString();}//获取随机颜色public static Color getRandomColor(){return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));}//返回某颜色的反色public static Color getReverseColor(Color c){return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());}public void doGet(HttpServletRequest request,HttpServletResponse response) {response.setContentType("image/jpeg");String randomString = getRandomString();request.getSession(true).setAttribute("randomString", randomString);//放到session中int width = 100;//图片宽度int height = 30;//图片高度Color color = getRandomColor();//随机颜色用于背景色Color reverse = getReverseColor(color);//反色用于前景色BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建一个彩色图片Graphics2D g = bi.createGraphics();//获取绘图对象g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));//设置字体g.setColor(color);//设置颜色g.fillRect(0, 0, width, height);//绘制背景g.setColor(reverse);  //设置颜色g.drawString(randomString, 18, 20); //绘制随机字符串for (int i = 0 , n=random.nextInt(100); i < n; i++) {g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);//画最多100个噪点}ServletOutputStream out;try {out = response.getOutputStream();//转成JPEG格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);//编码器encoder.encode(bi);//对图片进行编码out.flush();//输出到客户端} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>IdentityServlet</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <servlet>  <servlet-name>IdentityServlet</servlet-name>  <servlet-class>com.wang.indentity.IdentityServlet</servlet-class>  <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>  <servlet-name>IdentityServlet</servlet-name>  <url-pattern>/servlet/IdentityServlet</url-pattern>  </servlet-mapping>    </web-app>
identity.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title><script>function reloadImage(){//document.getElementById('btn').disabled = true;document.getElementById('identity').src = 'servlet/IdentityServlet?ts='+new  Date().getTime();}</script></head><body><img  id = "identity" src="servlet/IdentityServlet" onload = "btn.disable = false;"><input type="button" value="换个图片" onclick = "reloadImage()" id="btn" ></body></html>




0 0
原创粉丝点击