servlet servlet请求与响应
2016-01-21 17:13
417 查看
request
客户端浏览器发出的请求被封装成一个HttpServletRequest对象。所有的信息包括请求的地址,请求的参数,提交的数据,上传的文件,客户端的Ip地址甚至客户端操作系统都包含在HttpServletRequest对象中。
response
servlet不仅能输出文本与图片,还能输出其他格式数据,例如word,excel,pdf,mps等
只要正确设定输出类型及输出流。不同的输出类型需要声明不同的Context-Type属性,例如JPG图片是“image/jpeg",而word则是”application/msword"。
实例,response生成图片验证码,response.setContentType("image/jpeg");
web.xml:
效果:
客户端浏览器发出的请求被封装成一个HttpServletRequest对象。所有的信息包括请求的地址,请求的参数,提交的数据,上传的文件,客户端的Ip地址甚至客户端操作系统都包含在HttpServletRequest对象中。
response
servlet不仅能输出文本与图片,还能输出其他格式数据,例如word,excel,pdf,mps等
只要正确设定输出类型及输出流。不同的输出类型需要声明不同的Context-Type属性,例如JPG图片是“image/jpeg",而word则是”application/msword"。
实例,response生成图片验证码,response.setContentType("image/jpeg");
package com.servlet; 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.ServletException; 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{ private static final long serialVersionUID = 1L; public static final char[] CHARS = {'2','3','4','5','6','7','8', '9','A','B','C','D','E','F','G','H','Z','K','L','M' ,'N','P','Q','R','S','T','U','V','W','X','Y','Z'}; //don't iclude 0,O,1,I,which difficult to identify public static Random random = new Random(); 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) throws ServletException,IOException{ //have to set the output type response.setContentType("image/jpeg"); String randomString = getRandomString(); //put it in the session request.getSession(true).setAttribute("randomString", randomString); //width of pic int width = 100; //heighth of pic int height = 30; //color of pic content Color color = getRandomColor(); //color of background Color reverse = getReverseColor(color); //create a colorful pic BufferedImage bi = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); //text style g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16)); g.setColor(color); //draw background g.fillRect(0, 0, width, height); g.setColor(reverse); //draw random text g.drawString(randomString, 18, 20); //draw the points for(int i = 0,n = random.nextInt(100); i < n; i++){ g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); } ServletOutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(bi); out.flush(); } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <servlet> <servlet-name>IdentyServlet</servlet-name> <servlet-class>com.servlet.IdentityServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>IdentyServlet</servlet-name> <url-pattern>/servlet/IdentityServlet</url-pattern></servlet-mapping> </web-app>
效果:
![](http://images2015.cnblogs.com/blog/726249/201601/726249-20160121171145500-127633119.jpg)
相关文章推荐
- duplicate symbols for architecture x86_64
- new\new[]\delete\delete[]区别
- 0121 --view 可以当作线
- thinkphp读取配置文件中的数据
- 【基础】——云平台
- OJ 系列之24点游戏算法
- 安全驾驶-携带拖车绳(三十一)
- zookeeper(三) 应用场景
- 如何解决iOS界面操作导致导致NSTimer暂停计时的问题?
- C#向Sql数据库插入控制
- ubuntu下安装交叉编译工具链
- tab选项卡
- 细数JDK里的设计模式<转>
- C#中static类型的变量和方法
- 电子拍卖系统开发第三天
- WPF禁止拖拽窗口到边缘自动最大化
- Android 天气应用开发
- SQL Server中TOP子句可能导致的问题以及解决办法
- ubuntu12.04 安装 opencv 2.4.8(非源代码编译)
- 整理的一些比较基础的面试知识点