java Web認証コード


web開発では、コードの使い方を検証するためによく使われます。みんなに例を書いてあげます。
注:利用した技術は主にHttpServletです。
jspホームページ:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head><!--          ,                  -->
  <script type="text/javascript">
  	function toreal(){
  		document.location.href="index.jsp";
  	}
  	
  </script>
  
  <body>
    <form action="" method="post">
    	<input type="text" name="username"><br/>
    	<input type="password" name="password"><br/>
    	<input type="text" name="code">
    	<img alt="   " src="/java_Code/servlet/code">
    	<a href="javascript:toreal()">   </a>
    	<br/>
    	<input type="submit" value="  "><br/><!--         ,      -->
    	
    </form>
  </body>
</html>
servletページで認証コードのピクチャコードを生成します。
package com.dp.java.code;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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;

/**
 *        
 *
 */
public class code extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//         
		response.setHeader("Expires", "-1");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "-1");
		
		int height=25;
		int width=120;
		//        BufferedImage
		BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//      
		Graphics g=img.getGraphics();
		//   drawRect         。
		g.drawRect(0, 0, width, height);
		//    
		g.setColor(Color.RED);
		g.fillRect(1, 1, width-2, height-2);
		//    
		g.setColor(Color.BLACK);
		Random r=new Random();
		for(int i=0;i<20;i++)
		g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
		//      
		g.setColor(Color.BLUE);
		g.setFont(new Font("    ", Font.BOLD|Font.ITALIC, 20));//BOLD  ,ITALIC  
		int d=15;
		for(int j=0;j<4;j++){
			g.drawString(r.nextInt(10)+"", d, 20);
			d+=20;
		}
		            		
		//   web  
		ImageIO.write(img, "jpg", response.getOutputStream());
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
最後はxmlマッピングファイルです。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>java_Code</display-name>
  <servlet>
    <servlet-name>code</servlet-name>
    <servlet-class>com.dp.java.code.code</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>code</servlet-name>
    <url-pattern>/servlet/code</url-pattern>
  </servlet-mapping>
 
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
結果: