您的位置:首页 > 其它

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");

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>


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: