2011年10月24日 星期一

驗證圖形驗證碼

index.jsp ----->主頁面

<%@ page language="java" contentType="text/html; charset=BIG5"%>

<form method="post" action="Verification.jsp">
    <img src="dynamicImage"/><br/>
    <input type="text" name="value"></input>
    <input type="submit"></input>
</form>


Verification.jsp ----->驗證

<%@ page language="java" contentType="text/html; charset=big5"%>

<%
    if( request.getParameter("value").equals(session.getAttribute("number")) ){
        out.print("驗證碼正確");
    }else{
        out.print("驗證碼錯誤");
    }
%>


DynamicImage.java ---->產生動態圖形驗證碼

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class DynamicImage extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("image/jpeg");

        BufferedImage bi = new BufferedImage(50,30,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, 50, 30);
        g2d.setColor(Color.black);
        g2d.drawRect(5, 5, 40, 20);

        StringBuilder sbl = new StringBuilder();
        while(sbl.length() < 4){
            sbl.append(Integer.toString((int)(Math.random() * 9)));
        }

        session.setAttribute("number", sbl.toString());
  
        g2d.setFont(new Font("Default",Font.BOLD,13));
        g2d.drawString(sbl.toString(), 10, 20);
  
        for(int i = 0; i < 20; i++){
            int x = (int)(Math.random() * 40) + 5;
            int y = (int)(Math.random() * 20) + 5;
   
            g2d.fillOval(x, y, 1, 1);
        }
  
        ServletOutputStream sos = response.getOutputStream();
  
        Iterator ite = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter iw = (ImageWriter)ite.next();
  
        ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
        iw.setOutput(ios);
        iw.write(bi);
        ios.flush();
        sos.close();
}


web.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_2_5.xsd" id="WebApp_ID" version="2.5">

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>DynamicImage</servlet-name>
    <servlet-class>com.morty.dynamicimage.DynamicImage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DynamicImage</servlet-name>
    <url-pattern>/dynamicImage</url-pattern>
  </servlet-mapping>

</web-app>

2011年10月23日 星期日

動態產生密碼圖片

DynamicImage.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DynamicImage extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        BufferedImage bi = new BufferedImage(50,30,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, 50, 30);
        g2d.setColor(Color.black);
        g2d.drawRect(5, 5, 40, 20);
  
        StringBuilder sbl = new StringBuilder();
        while(sbl.length() < 4){
                sbl.append(Integer.toString((int)(Math.random() * 9)));
        }
  
        g2d.setFont(new Font("Default",Font.BOLD,13));
        g2d.drawString(sbl.toString(), 10, 20);
  
        for(int i = 0; i < 20; i++){
                int x = (int)(Math.random() * 40) + 5;
                int y = (int)(Math.random() * 20) + 5;
   
                g2d.fillOval(x, y, 1, 1);
        }
  
        response.setContentType("image/jpeg");
        ServletOutputStream sos = response.getOutputStream();
  
        Iterator ite = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter iw = (ImageWriter)ite.next();
  
        ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
        iw.setOutput(ios);
        iw.write(bi);
        ios.flush();
        sos.close();
    }
}

2011年10月3日 星期一

BMI指數

BMI.jsp


<%@ page contentType="text/html; charset=big5"%>

<form method="post" action="BMI.jsp">
    性別:<select name="sex">
        <option></option>
        <option value="man">男</option>
        <option value="woman">女</option>
    </select><br>
    身高(公分):<input type="text" name="height" style="width:5%"/><br>
    體重(公斤):<input type="text" name="weight" style="width:5%"/><br>
    <input type="submit" value="送出">
</form>

<%
    String height = request.getParameter("height");
    String weight = request.getParameter("weight");
    if(height == null || weight == null){
        height = "0.0";
        weight = "0.0";
    }
    String url = "count.jsp?height=" + height + "&weight=" + weight;
%>

<jsp:include page="<%=url%>"></jsp:include>


count.jsp


<%@ page contentType="text/html; charset=big5" %>
<%@
    page import="java.io.*"
%>

<%
    String exponent = "";
    String standard = "";
    int num = 0;
    double h=0.0,w=0.0;

    h = Double.parseDouble(request.getParameter("height")) / 100;
    w = Double.parseDouble(request.getParameter("weight"));

    double ans = w / (Math.pow(h, 2)); //體重 (kg) / 身高 (m)的平方

    if( Double.isNaN(ans) == false){
        exponent = Long.toString(Math.round(ans));
        num = Integer.parseInt(exponent);

        /*
            男性: (身高cm-80)×70﹪=標準體重
            女性: (身高cm-70)×60﹪=標準體重
        */
        if(request.getParameter("sex").equals("man")){
            standard = Double.toString(((h*100)-80)*0.7) + " 公斤";
        }else{
            standard = Double.toString(((h*100)-70)*0.6) + " 公斤";
        }
    }

    String str = "";

    //結果
    if( num == 0){
        str = "";
    }else if( num < 18.5 ){
        str = "體重過輕";
    }else if( num > 18.5 && num < 24){
        str = "正常範圍";
    }else if( num >= 24 && num < 27){
        str = "體重過重";
    }else if( num >= 27 && num < 30){
        str = "輕度肥胖";
    }else if( num >= 30 && num < 35 ){
        str = "中度肥胖";
    }else{
        str = "重度肥胖";
    }

%>

<p>BMI 指數:<%=exponent%></p>

<p>結果:<%=str%></p>

<p>標準體重:<%=standard%></p>


web.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_2_5.xsd" version="2.5">

    <welcome-file-list>
        <welcome-file>BMI.jsp</welcome-file>
    </welcome-file-list>
</web-app>