Servlet 3 HttpServletRequest HttpServletResponse 验证码图片 form表单

目录:

HttpServletRequest:
获得请求行 和 客户机信息
获得请求头中referer信息,防止盗链
获得form提交数据
请求重定向 转发
RequestDispatcher.include包含
HttpServletResponse
生成状态码和头信息
从response/demo2.html 请求中 获得用户名和密码 判断是否正确
定时自动跳转网页
禁用浏览器缓存
生成HTML网页
生成XML
用Servlet程序生成一张验证码图片

工程目录:

获得请求行 和 客户机信息

package cn.itcast.servlet.request;

import java.io.IOException;

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

/**
 * 获得请求行 和 客户机信息
 * 
 * @author seawind
 * 
 */
public class RequestServlet1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获得请求方式
        System.out.println("请求方式:" + request.getMethod());
        // 获得协议
        System.out.println("协议:" + request.getProtocol());
        // 获得请求资源路径
        System.out.println("请求路径:" + request.getRequestURI());
        System.out.println("请求路径:" + request.getRequestURL());
        // 获得请求行中参数
        System.out.println("参数:" + request.getQueryString());
        // 打印客户机IP
        System.out.println("ip:" + request.getRemoteAddr());
        // 获得工程虚拟路径名称
        System.out.println("虚拟路径名称:" + request.getContextPath());
    }

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

}

获得请求头中referer信息,防止盗链

html

<!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>
</head>
<body>
<h1>正常访问页面,从这个页面访问 不属于盗链</h1>
<a href="/day6/request2" >访问重要资源</a>
</body>
</html>

Servlet

package cn.itcast.servlet.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;

/**
 * 获得请求头中referer信息,防止盗链
 * 
 * @author seawind
 * 
 */
public class RequestServlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 遍历所有请求头信息
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            System.out.println(name + ":" + request.getHeader(name));
        }
        System.out.println("-----------------------------------------");

        System.out.println("您使用浏览器:" + request.getHeader("user-agent"));

        String referer = request.getHeader("referer");
        // 判断referer是否存在和有效
        if (referer != null && referer.startsWith("http://localhost/day6")) {
            // 不属于盗链
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("机密信息");
        } else {
            // 盗链
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("您的请求是盗链");
        }
    }

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

}

获得form提交数据

<!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>
</head>
<body>
<h1>最复杂form表单</h1>
<form method="get" action="/day6/request3">
    用户名 <input type="text" name="username" /><br/>
    密码 <input type="password" name="password" /><br/>
    性别 <input type="radio" name="gender" value="男" /> 男 <input type="radio" name="gender" value="女" />女 <br/>
    爱好 <input type="checkbox" name="hobby" value="体育" />体育
    <input type="checkbox" name="hobby" value="音乐" />音乐
    <input type="checkbox" name="hobby" value="读书" />读书<br/>
    城市 <select name="city">
        <option value="北京">北京</option>
        <option value="上海">上海</option>
        <option value="广州">广州</option>
    </select> <br/>
    个人简介 <textarea rows="5" cols="60" name="introduce"></textarea><br/>
    <input type="submit" value="提交" />
</form>
</body>
</html>

Servlet

package cn.itcast.servlet.request;

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

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

/**
 * 获得form提交数据
 * 
 * @author seawind
 * 
 */
public class RequestServlet3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // POST提交 处理乱码
        // request.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        // 校验username 有效
        if (username != null && username.length() > 0) {
            System.out.println("username 有效");
        }

        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        // 使用getParameter只能获得一个值
        // String hobby = request.getParameter("hobby");
        // 获得多个请求参数值
        String[] hobby = request.getParameterValues("hobby");

        String city = request.getParameter("city");
        String introduce = request.getParameter("introduce");

        // 对username 采用手动编解码 处理get乱码
        username = URLEncoder.encode(username, "ISO-8859-1");
        username = URLDecoder.decode(username, "utf-8");

        // 对gender用 另一个编解码方式
        gender = new String(gender.getBytes("ISO-8859-1"), "utf-8");

        System.out.println("姓名:" + username);
        System.out.println("密码:" + password);
        System.out.println("性别:" + gender);
        System.out.println("爱好:" + Arrays.toString(hobby));
        System.out.println("城市:" + city);
        System.out.println("个人介绍:" + introduce);

        // 使用parametermap 遍历所有请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> keySet = parameterMap.keySet();
        for (String parameterName : keySet) {
            System.out.println(parameterName + ": "
                    + Arrays.toString(parameterMap.get(parameterName)));
        }

    }

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

}

请求重定向 转发

package cn.itcast.servlet.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;

/**
 * 执行 Servlet4后执行Servlet5
 * 
 * @author seawind
 * 
 */
public class RequestServlet4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().println("servlet4");
        System.out.println("request servlet 4 ...");
        // 重定向
        // response.sendRedirect("/day6/request5");

        // 转发
        // 转发操作过程中,可以通过request传递数据
        request.setAttribute("name", "传智播客");// 保存request对象
        RequestDispatcher dispatcher = request
                .getRequestDispatcher("/request5");// 服务器内部行为
        dispatcher.forward(request, response);
    }

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

}
package cn.itcast.servlet.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 RequestServlet5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().println("servlet5");
        System.out.println("request servlet 5 ...");
        // 取出保存request中数据
        String name = (String) request.getAttribute("name");
        System.out.println("取出name : " + name);
    }

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

}

RequestDispatcher.include包含

package cn.itcast.servlet.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;

/**
 * 生成一个网页,将logo.html 、menu.html、footer.html 都包含进行
 * 
 * @author seawind
 * 
 */
public class RequestServlet6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获得Dispatcher转发器
        RequestDispatcher dispatcher1 = request
                .getRequestDispatcher("/request/demo6/logo.html");
        dispatcher1.include(request, response);

        RequestDispatcher dispatcher2 = request
                .getRequestDispatcher("/request/demo6/menu.html");
        dispatcher2.include(request, response);

        // 输出正文信息,引用三个页面默认采用response.getOutputStream输出的
        response.getOutputStream().println("<h1>Content</h1>");

        RequestDispatcher dispatcher3 = request
                .getRequestDispatcher("/request/demo6/footer.html");
        dispatcher3.include(request, response);
    }

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

}

HttpServletResponse
生成状态码和头信息

package cn.itcast.servlet.response;

import java.io.IOException;

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

/**
 * 生成状态码和头信息
 * 
 * @author seawind
 * 
 */
public class ResponseServlet1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 生成状态码
        // response.setStatus(500); // 设置不同状态码

        // 结合302状态码和 Location头信息 ,完成请求重定向
        response.setStatus(302);
        response.setHeader("Location", "/day6/hello.html");// location值重定向后资源路径
    }

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

}

从response/demo2.html 请求中 获得用户名和密码 判断是否正确

html

<!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>
</head>
<body>
<h1>登陆页面,密码错误完成重定向</h1>
<form action="/day6/response2" method="post">
    用户名 <input type="text" name="username" /><br/>
    密码 <input type="password" name="password" /><br/>
    <input type="submit" value="登陆" />
</form>
</body>
</html>
package cn.itcast.servlet.response;

import java.io.IOException;

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

/**
 * 从response/demo2.html 请求中 获得用户名和密码 判断是否正确
 * 
 * @author seawind
 * 
 */
public class ResponseServlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获得请求中 用户名和密码
        String username = request.getParameter("username");
        // 字符串要为form表单对象name属性
        String password = request.getParameter("password");

        // 假设admin/admin 就是用户名和密码
        if (username.equals("admin") && password.equals("admin")) {
            // 登陆成功
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("登陆成功!");
        } else {
            // 用户名或者密码错误 ---- 重定向 登陆页面
            // response.setStatus(302);
            // response.setHeader("Location", "/day6/response/demo2.html");
            response.sendRedirect("/day6/response/demo2.html");
        }
    }

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

}

定时自动跳转网页

package cn.itcast.servlet.response;

import java.io.IOException;

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

/**
 * 定时自动刷新网页
 * 
 * @author seawind
 * 
 */
public class ResponseServlet3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("refresh", "5;url=/day6/hello.html");

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("页面会在5秒后 自动跳转hello.html");

    }

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

}

html中meta标签 可以实现和HTTP响应头信息设置相同效果

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- meta标签 可以实现和HTTP响应头信息设置相同效果 -->
<meta http-equiv="refresh" content="5;url=/day6/hello.html" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    var i = 5;
    function init(){
        document.getElementById("mytimes").innerHTML = i;
        // 每隔1秒重复调用 init方法 i-- 
        i--;
        // 通过window 内置对象 setTimeOut 完成每隔1秒重复调用
        window.setTimeout("init();", 1000);
    }
</script>
</head>
<!-- 在页面加载完成后 执行init 方法 -->
<body onload="init();">
<h1>HTML中通过Meta实现<span id="mytimes"></span>秒后自动跳转hello.html</h1>
</body>
</html>

禁用浏览器缓存

package cn.itcast.servlet.response;

import java.io.IOException;
import java.util.Date;

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

/**
 * 禁用浏览器缓存
 * 
 * @author seawind
 * 
 */
public class ResponseServlet4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置response 的三个头信息
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        // Expires 要求格式 日期格式
        // 通过setDateHeader设置一个毫秒信息,自动转换日期格式
        response.setDateHeader("Expires", -1);

        response.getWriter().println("AAAAA" + new Date().toLocaleString());
    }

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

}

生成HTML网页

package cn.itcast.servlet.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;

/**
 * 生成HTML网页
 * 
 * @author seawind
 * 
 */
public class ResponseServlet5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置字符集
        // response.setCharacterEncoding("utf-8");

        // 设置文件类型,通知浏览器查看编码方式
        response.setContentType("text/html;charset=gbk");

        // 获得响应字符输出流
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<body>");
        out.println("Servlet生成网页");
        out.println("</body>");
        out.println("</html>");

        // 响应输出流 会自动关闭 (由tomcat服务器关闭 )

    }

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

}

生成XML

package cn.itcast.servlet.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;

/**
 * 生成XML
 * 
 * @author seawind
 * 
 */
public class ResponseServlet6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置文件类型和字符集
        response.setContentType("text/xml;charset=utf-8");
        // 获得响应输出流
        PrintWriter out = response.getWriter();

        // 打印XML内容
        out.println("<book>");
        out.println("<name>java编程思想</name>");
        out.println("<price>80</price>");
        out.println("</book>");
    }

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

}

用Servlet程序生成一张验证码图片

html

<!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 type="text/javascript">
    // 点击切换验证码原理 ,重新载入图片
    function change(){
        // 获得id为myimg对象 ,重新加载Servlet程序
        document.getElementById("myimg").src= "/day6/response7?"+new Date().getTime();
    }
</script>
</head>
<body>
<!-- 引用一张图片,显示网页上 -->
<img src="/day6/response7" onclick="change();" id="myimg" style="cursor: pointer;"/>
</body>
</html>

Servlet

package cn.itcast.servlet.response;

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.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用Servlet程序生成一张验证码图片
 * 
 * @author seawind
 * 
 */
public class ResponseServlet7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 禁止浏览器缓存 验证码图片
        // response.setHeader("cache-control", "no-cache");
        // response.setHeader("pragma", "no-cache");
        // response.setDateHeader("expires", -1);

        // 生成图片。使用Java图形界面技术awt、swing 包
        // 1 创建内存中图片
        int width = 120;
        int height = 30;
        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);

        // 2 绘制图片背景色
        Graphics2D graphics2d = (Graphics2D) bufferedImage.getGraphics();
        // 指定颜色
        graphics2d.setColor(Color.GRAY);
        graphics2d.fillRect(0, 0, width, height);

        // 3、绘制边框
        graphics2d.setColor(Color.yellow);
        graphics2d.drawRect(0, 0, width - 1, height - 1);

        // 4、向图片中生成验证码内容
        graphics2d.setColor(Color.BLUE);
        graphics2d.setFont(new Font("新宋体", Font.BOLD, 18));
        // String word =
        // "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        // 如果使用汉字,字体必须要支持汉字
        String word = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\u6797\u505c\u606f\u53e5\u533a\u8863\u822c\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6";
        Random random = new Random();
        int x = 5;
        for (int i = 0; i < 4; i++) {
            // 加入字体旋转 角度-30 --- 30之间
            int jiaodu = random.nextInt(60) - 30;
            // 转换角度为弧度
            double theta = jiaodu * Math.PI / 180;

            int randomIndex = random.nextInt(word.length());// 生成下标
            char c = word.charAt(randomIndex);
            // 将字符写入图片
            graphics2d.rotate(theta, x, 20);
            graphics2d.drawString(c + "", x, 20);
            graphics2d.rotate(-theta, x, 20);
            x += 30;
        }

        // 5 绘制干扰线
        graphics2d.setColor(Color.LIGHT_GRAY);
        int x1;
        int x2;
        int y1;
        int y2;
        for (int i = 0; i < 10; i++) {
            x1 = random.nextInt(width);
            x2 = random.nextInt(width);
            y1 = random.nextInt(height);
            y2 = random.nextInt(height);

            graphics2d.drawLine(x1, y1, x2, y2);
        }
        // 释放内存中资源
        graphics2d.dispose();

        // 生成图片到浏览器
        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
    }

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

    public static void main(String[] args) {
        System.out.println('\u9fa5');
        System.out.println('\u9fa4');
        System.out.println('\u9fa3');
    }

}

 

posted @ 2014-08-20 17:57  vaer  阅读(325)  评论(0编辑  收藏  举报