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.