Java learning notes-verification code implementation (15), java learning notes

Source: Internet
Author: User

Java learning notes-verification code implementation (15), java learning notes

The main task for the occurrence of the Verification Code is to solve the problem of repeated submission of forms.

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 0. the notification browser cannot cache the image data response. setHeader ("expires", "-1"); response. setHeader ("cache-control", "no-cache"); response. setHeader ("pragma", "no-cache"); // 1. create an image object BufferedImage img = new BufferedImage (80, 20, BufferedImage. TYPE_INT_RGB); ServletOutputStream out = response. getOutputStream (); // 2. obtain the image Graphics pic = img. getGraphics (); // 3. set the background to white pic. setColor (Color. WHITE); pic. fillRect (0, 0, 80, 20); // 4. set the image pic. setColor (Color. RED); pic. setFont (new Font (null, Font. BOLD, 12); // 5. add data pic. drawString (makeString (4), 5, 15); // 6. output the image to the browser ImageIO. write (img, "jpeg", out );}
The response body output from the response stream.
Public class Demo3 extends HttpServlet {String str = null; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// sets the response stream encoding method response. setCharacterEncoding ("UTF-8"); // notify the browser to decode response in the specified encoding method. setContentType ("text/html; charset = UTF-8"); // gets the response Stream object PrintWriter out = response. getWriter (); // defines the output data String data = "hello <br/>"; // output String data out. write (data); // output page data = "

What is the difference between write () and prinln?

Different output numbers: write () converts output numbers to characters, and println is output as is.

Different outputs of null: When write () Outputs reference types, toString data called is converted to String data. Therefore, if the object is null, a null pointer exception is thrown directly. Println directly outputs null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.