微信公衆號(答題抽獎)開發

首先在公衆號裏面設置好連接地址。javascript

接下來開始開發:css

連接地址的頁面,也就是首頁,須要調微信接口獲取code:html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>話費抽獎</title>
</head>
<body onLoad="parent.location='https://open.weixin.qq.com/connect/oauth2/authorize?appid=*****************&redirect_uri=http://survey.runmin.top/uploa/wx.do&response_type=code&scope=snsapi_base'">
</body>
</html>

注意這裏的redirect_uri就是跳轉的地址,而且微信會帶着code返回,也就是說request中有code參數。java

注意我這裏web.xml裏面配置了多個servlet,包括wx.do:mysql

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>uploa</display-name>
  <servlet>
    <servlet-name>ImageUpload</servlet-name>
    <servlet-class>com.bt.action.ImageUpload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ImageUpload</servlet-name>
    <url-pattern>/uploadImage</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Wx</servlet-name>
    <servlet-class>com.bt.action.Wx</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Wx</servlet-name>
    <url-pattern>/wx.do</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>UserScore</servlet-name>
    <servlet-class>com.bt.action.UserScore</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserScore</servlet-name>
    <url-pattern>/UserScore.do</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>UserPrize</servlet-name>
    <servlet-class>com.bt.action.UserPrize</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserPrize</servlet-name>
    <url-pattern>/UserPrize.do</url-pattern>
  </servlet-mapping>
    <servlet>
    <servlet-name>GetPercent</servlet-name>
    <servlet-class>com.bt.action.GetPercent</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GetPercent</servlet-name>
    <url-pattern>/GetPercent.do</url-pattern>
  </servlet-mapping>
      <servlet>
    <servlet-name>GetPrice</servlet-name>
    <servlet-class>com.bt.action.GetPrice</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GetPrice</servlet-name>
    <url-pattern>/GetPrice.do</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

因此調完微信之後會跳轉到個人com.bt.action.Wx這個servlet(亂碼沒有作處理,比較麻煩):jquery

package com.bt.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

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

import org.json.JSONException;
import org.json.JSONObject;

public class Wx extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private  String APPID = "wxb6fb629b1400d10f";// �����������appid����URL�����appid��һ����˼
    private  String APPSECRET = "2ae8ba03be5bccd5190f9f65b202768a"; // ����������ŵ�app��Կ
    private  String ACCESS_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code"; // ����������ȡ�û���Ϣ��URL
    /**
     * @see HttpServlet#HttpServlet()
     */


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String code = request.getParameter("code");// ��ȡOAuth2.0����󣬷��������ص�code����,���code�ڽ�������΢�ŷ��������û���Ϣ��ʱ��Ҫ�õ�
        System.out.println(code);
        String requestUrlString = ACCESS_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET).replace("CODE",
                code);// �������û���URL�е�///�����滻������������
        System.out.println(requestUrlString);
        URL url = new URL(requestUrlString); // ����url����
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // ������
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("GET");
        urlConnection.setUseCaches(false);
        urlConnection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
        StringBuffer buffer = new StringBuffer();// �洢���������ص���Ϣ
        String line = "";
        String openid = "";
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String result = buffer.toString();
        System.out.println(result);
        try {
            JSONObject resultObject = new JSONObject(result); // �����������ص��ַ���ת����json��ʽ
            openid = resultObject.getString("openid");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        //��ȡaccess_tokenֵ
//        URL url0 = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET+""); // ����url����
//        HttpURLConnection urlConnection0 = (HttpURLConnection) url0.openConnection(); // ������
//        urlConnection0.setDoOutput(true);
//        urlConnection0.setDoInput(true);
//        urlConnection0.setRequestMethod("GET");
//        urlConnection0.setUseCaches(false);
//        urlConnection0.connect();
//        BufferedReader reader0 = new BufferedReader(new InputStreamReader(urlConnection0.getInputStream(), "utf-8"));
//        StringBuffer buffer0 = new StringBuffer();// �洢���������ص���Ϣ
//        String line0 = "";
//        String access_token="";
//        String expires_in = "";
//        while ((line0 = reader0.readLine()) != null) {
//            buffer0.append(line0);
//        }
//        String result0 = buffer.toString();
//        System.out.println(result0);
//        try {
//            JSONObject resultObject = new JSONObject(result0); // �����������ص��ַ���ת����json��ʽ
//            expires_in = resultObject.getString("expires_in");
//            access_token = resultObject.getString("access_token");
//        } catch (JSONException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        System.err.println("access_token����ʱ�䣨�룩:"+expires_in);
//        
//        
//        URL url2 = new URL("https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openid+"&lang=zh_CN"); // ����url����
//        System.out.println(url2);
//        HttpURLConnection urlConnection2 = (HttpURLConnection) url2.openConnection(); // ������
//        urlConnection2.setDoOutput(true);
//        urlConnection2.setDoInput(true);
//        urlConnection2.setRequestMethod("GET");
//        urlConnection2.setUseCaches(false);
//        urlConnection2.connect();
//        BufferedReader reader2 = new BufferedReader(new InputStreamReader(urlConnection2.getInputStream(), "utf-8"));
//        StringBuffer buffer2 = new StringBuffer();// �洢���������ص���Ϣ
//        String line2 = "";
//        String nickname = "";
//        while ((line2 = reader2.readLine()) != null) {
//            buffer2.append(line2);
//        }
//        String result2 = buffer2.toString();
//        System.out.println(result2);
//        try {
//            JSONObject resultObject = new JSONObject(result); // �����������ص��ַ���ת����json��ʽ
//            nickname = resultObject.getString("nickname");
//        } catch (JSONException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        
        HttpSession session=request.getSession();
        session.setAttribute("username", openid);
        request.getRequestDispatcher("index.html").forward(request, response);
    }

}

接下來跳轉到index.html:css3

<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:w="urn:schemas-microsoft-com:office:word"
    xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta name="viewport"
    content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 14">
<meta name=Originator content="Microsoft Word 14">
<title>憲法答題</title>
<style>
@font-face {
    font-family: "Times New Roman";
}

@font-face {
    font-family: "宋體";
}

@font-face {
    font-family: "Wingdings";
}

@font-face {
    font-family: "Calibri";
}

@font-face {
    font-family: "微軟雅黑";
}

p.MsoNormal {
    mso-style-name: 正文;
    mso-style-parent: "";
    margin: 0pt;
    margin-bottom: .0001pt;
    mso-pagination: none;
    text-align: justify;
    text-justify: inter-ideograph;
    font-family: Calibri;
    mso-fareast-font-family: 宋體;
    mso-bidi-font-family: 'Times New Roman';
    font-size: 22.5000pt;
    mso-font-kerning: 1.0000pt;
}

h1 {
    mso-style-name: "標題 1";
    mso-style-next: 正文;
    margin-top: 5.0000pt;
    margin-bottom: 5.0000pt;
    mso-margin-top-alt: auto;
    mso-margin-bottom-alt: auto;
    mso-pagination: none;
    text-align: left;
    font-family: 宋體;
    font-weight: bold;
    font-size: 26.0000pt;
    mso-font-kerning: 22.0000pt;
}

span.10 {
    font-family: 'Times New Roman';
}

span.15 {
    font-family: 'Times New Roman';
    color: rgb(0, 0, 255);
    text-decoration: underline;
    text-underline: single;
}

p.p {
    mso-style-name: "普通\(網站\)";
    margin-top: 5.0000pt;
    margin-right: 0.0000pt;
    margin-bottom: 5.0000pt;
    margin-left: 0.0000pt;
    mso-margin-top-alt: auto;
    mso-margin-bottom-alt: auto;
    mso-pagination: none;
    text-align: left;
    font-family: Calibri;
    mso-fareast-font-family: 宋體;
    mso-bidi-font-family: 'Times New Roman';
    font-size: 22.0000pt;
}

span.msoIns {
    mso-style-type: export-only;
    mso-style-name: "";
    text-decoration: underline;
    text-underline: single;
    color: blue;
}

span.msoDel {
    mso-style-type: export-only;
    mso-style-name: "";
    text-decoration: line-through;
    color: red;
}

table.MsoNormalTable {
    mso-style-name: 普通表格;
    mso-style-parent: "";
    mso-style-noshow: yes;
    mso-tstyle-rowband-size: 0;
    mso-tstyle-colband-size: 0;
    mso-padding-alt: 0.0000pt 5.4000pt 0.0000pt 5.4000pt;
    mso-para-margin: 0pt;
    mso-para-margin-bottom: .0001pt;
    mso-pagination: widow-orphan;
    font-family: 'Times New Roman';
    font-size: 22.0000pt;
    mso-ansi-language: #0400;
    mso-fareast-language: #0400;
    mso-bidi-language: #0400;
}

@page {
    mso-page-border-surround-header: no;
    mso-page-border-surround-footer: no;
}

@page Section0 {
    margin-top: 72.0000pt;
    margin-bottom: 72.0000pt;
    margin-left: 90.0000pt;
    margin-right: 90.0000pt;
    size: 595.3000pt 841.9000pt;
    layout-grid: 15.6000pt;
}

div.Section0 {
    page: Section0;
}
</style>
<script type="text/javascript">
function toresult() {
    document.getElementById("form1").submit();
}

</script>
<script type="text/javascript"
    src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<!-- 屏蔽分享  -->
<script>
    
    function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
    }

    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
                    false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady();
    }
    var errori ='<%=request.getParameter("error")%>';
      if(errori=='yes'){
       alert("登陸失敗!");
      }
      
      function getRTime(){ 
          var EndTime= new Date('2017/12/7 23:59:59'); //截止時間 
          var NowTime = new Date(); 
          var t =EndTime.getTime() - NowTime.getTime(); 
          /*var d=Math.floor(t/1000/60/60/24); 
          t-=d*(1000*60*60*24); 
          var h=Math.floor(t/1000/60/60); 
          t-=h*60*60*1000; 
          var m=Math.floor(t/1000/60); 
          t-=m*60*1000; 
          var s=Math.floor(t/1000);*/
           
          var d=Math.floor(t/1000/60/60/24); 
          var h=Math.floor(t/1000/60/60%24); 
          var m=Math.floor(t/1000/60%60); 
          var s=Math.floor(t/1000%60); 
           
          document.getElementById("t_d").innerHTML = d + ""; 
          document.getElementById("t_h").innerHTML = h + ""; 
          document.getElementById("t_m").innerHTML = m + ""; 
          document.getElementById("t_s").innerHTML = s + ""; 
          } 
          setInterval(getRTime,1000); 
          
</script>
</head>
<body style="tab-interval: 21pt; text-justify-trim: punctuation;">

    <!--StartFragment-->
    <form action="uploadImage" id="form1" name="form1" method="post">
        <div class="Section0"
            style="layout-grid: 15.6000pt; padding-left: 10px;" align=center>
            <p class=p align=center
                style="margin-top: 15.0000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: center; mso-line-height-alt: 12pt;">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(76, 76, 76); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 20.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);"><font
                    face="微軟雅黑">第一期憲法答題</font></span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; font-size: 14.0000pt; mso-font-kerning: 0.0000pt;"><o:p></o:p></span>
            </p>
            <samp style="color: red; font-size: 14.0000pt">完成答題可參與抽獎!抽中獎後,話費將在48小時內充值成功。請注意查收。每一個微信號只能參與本週題目的一次機會答題抽獎。</samp>
            <p class=p
                style="margin-top: 15.0000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan;">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);"><font
                    face="微軟雅黑">歡迎參與本次問卷</font></span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; font-size: 14.0000pt; mso-font-kerning: 0.0000pt;"><o:p></o:p></span>
            </p>
            <p class=p
                style="margin-top: 15.0000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan;">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);"><font
                    face="微軟雅黑">離答題結束還有: <span id="t_d">00天</span> <span
                        id="t_h">00時</span> <span id="t_m">00分</span> <span id="t_s">00秒</span></span>
            </p>


            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: -11.3000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">一、新中國的第一部憲法是:</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val1" value="A"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">A、共同綱領</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val1" value="B"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">B、1954年憲法</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val1" value="C"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">C、1975年憲法</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val1" value="D"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">D、1982年憲法</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: -11.3000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">二、我國憲法規定,土地使用權能夠依照法律的規定:</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val2" value="A"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">A、出租</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val2" value="B"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">B、買賣</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val2" value="C"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">C、轉讓</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val2" value="D"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">D、饋贈
                </span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: -11.3000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">三、中華共和國的一切權力屬於:</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val3" value="A"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">A、公民</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val3" value="B"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">B、人民</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val3" value="C"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">C、人民表明大會</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val3" value="D"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">D、工農聯盟</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: -11.3000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">四、國家保護城鄉集體經濟組織的合法的權利和利益,(
                    )集體經濟的發展。</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val4" value="A"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">A、鼓勵、支持和引導</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val4" value="B"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">B、指導、幫助和監督</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val4" value="C"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">C、引導、監督和管理</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val4" value="D"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">D、鼓勵、指導和幫助</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: -11.3000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">五、我國現行憲法規定,父母有撫養教育未成年子女的義務,</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 14.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val5" value="A"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">A、子女有贍養扶助父母的義務</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val5" value="B"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">B、成年子女有贍養扶助年老父母的義務</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val5" value="C"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">C、子女有贍養扶助喪失勞動能力的父母的義務</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal
                style="margin-top: 22.5000pt; margin-right: 0.0000pt; margin-bottom: 7.5000pt; margin-left: 0.0000pt; text-indent: 0.0000pt; padding: 0pt 0pt 0pt 0pt; mso-pagination: widow-orphan; text-align: left; background: rgb(255, 255, 255);">
                <span></span><input type="radio" name="val5" value="D"><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(255, 255, 255); mso-shading: rgb(255, 255, 255);">D、成年子女有贍養扶助父母的義務</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(102, 102, 102); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal align=center onclick="toresult()"
                style="margin-top: 0.0000pt; cursor: hand; margin-right: 1.5000pt; margin-bottom: 0.0000pt; margin-left: 15.0000pt; text-indent: 0.0000pt; border-top: 1.0000pt solid rgb(83, 164, 244); mso-border-top-alt: 0.7500pt solid rgb(83, 164, 244); border-right: 1.0000pt solid rgb(83, 164, 244); mso-border-right-alt: 0.7500pt solid rgb(83, 164, 244); border-bottom: 1.0000pt solid rgb(83, 164, 244); mso-border-bottom-alt: 0.7500pt solid rgb(83, 164, 244); border-left: 1.0000pt solid rgb(83, 164, 244); mso-border-left-alt: 0.7500pt solid rgb(83, 164, 244); padding: 3pt 21pt 3pt 21pt; mso-pagination: widow-orphan; text-align: center; vertical-align: baseline; line-height: 21.0000pt; background: rgb(83, 164, 244);">
                <span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(255, 255, 255); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 10.5000pt; mso-font-kerning: 0.0000pt; background: rgb(83, 164, 244); mso-shading: rgb(83, 164, 244);">&#160;提&nbsp;交&#160;答&nbsp;</span><span
                    style="mso-spacerun: 'yes'; font-family: 微軟雅黑; color: rgb(120, 120, 120); letter-spacing: 0.0000pt; font-weight: normal; text-transform: none; font-style: normal; font-size: 9.0000pt; mso-font-kerning: 1.0000pt;"><o:p></o:p></span>
            </p>
            <p class=MsoNormal>
                <span
                    style="mso-spacerun: 'yes'; font-family: Calibri; mso-fareast-font-family: 宋體; mso-bidi-font-family: 'Times New Roman'; font-size: 10.5000pt; mso-font-kerning: 1.0000pt;"><o:p>&nbsp;</o:p></span>
            </p>
        </div>
    </form>
    <!--EndFragment-->
</body>
</html>

跳轉到ImageUpload:web

package com.bt.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class ImageUpload extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    @SuppressWarnings("static-access")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
//        String nickname=session.getAttribute("nickname").toString();
        String val1 = request.getParameter("val1");
        String val2 = request.getParameter("val2");
        String val3 = request.getParameter("val3");
        String val4 = request.getParameter("val4");
        String val5 = request.getParameter("val5");
        List<String> list = new ArrayList<>();
        int right = 0;
        int wrong = 0;
        int score = 0;
        if ("B".equals(val1)) {
            list.add("1.√");
            right = right + 1;
            score = score + 20;
        } else {
            list.add("1.<span style=\"font-size: 30.0000pt; color: red;\">×</span>。正確答案爲:新中國的第一部憲法是<span style=\"font-size: 30.0000pt; color: red;\">(B.1954年憲法)</span>");
            wrong = wrong + 1;
        }

        if ("C".equals(val2)) {
            list.add("2.√");
            right = right + 1;
            score = score + 20;
        } else {
            list.add("2.<span style=\"font-size: 30.0000pt; color: red;\">×</span>。正確答案爲: 我國憲法規定,土地使用權能夠依照法律的規定<span style=\"font-size: 30.0000pt; color: red;\">(C. 轉讓)</span>");
            wrong = wrong + 1;
        }

        if ("B".equals(val3)) {
            list.add("3.√");
            right = right + 1;
            score = score + 20;
        } else {
            list.add("3.<span style=\"font-size: 30.0000pt; color: red;\">×</span>。正確答案爲:中華共和國的一切權力屬於<span style=\"font-size: 30.0000pt; color: red;\">(B.人民)</span>");
            wrong = wrong + 1;
        }

        if ("D".equals(val4)) {
            list.add("4.√");
            right = right + 1;
            score = score + 20;
        } else {
            list.add("4.<span style=\"font-size: 30.0000pt; color: red;\">×</span>。正確答案爲: 國家保護城鄉集體經濟組織的合法的權利和利益,<span style=\"font-size: 30.0000pt; color: red;\">(D.鼓勵、指導和幫助)</span>集體經濟的發展。");
            wrong = wrong + 1;
        }

        if ("B".equals(val5)) {
            list.add("5.√");
            right = right + 1;
            score = score + 20;
        } else {
            list.add("5.<span style=\"font-size: 30.0000pt; color: red;\">×</span>。正確答案爲:我國現行憲法規定,父母有撫養教育未成年子女的義務,<span style=\"font-size: 30.0000pt; color: red;\">(B.成年子女有贍養扶助年老父母的義務)</span>");
            wrong = wrong + 1;
        }
        request.setAttribute("list", list);
        request.setAttribute("right", right);
        request.setAttribute("wrong", wrong);
        request.setAttribute("score", score);
        UserScore dao=new UserScore();
        try {
            boolean status = dao.doPost(request, response);
            if(status==false) {
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write("您已經答過題!");
                 RequestDispatcher dispatcher = request.getRequestDispatcher("NewFile.html");    // 使用req對象獲取RequestDispatcher對象
                    dispatcher.forward(request, response); 
            }else {
                request.getRequestDispatcher("result.jsp").forward(request, response);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}
跳轉到result.jsp:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>答題結果</title>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<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">
    -->
<script type="text/javascript"
    src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>


<!-- 屏蔽分享  -->
<script>
    function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
    }

    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
                    false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } else {
        onBridgeReady();
    }
    
     var choujiang = function (){

            $.post("UserScore.do",
            {
                score:score
              },
              function(data,status){
                console.log("Data: " + data + "\nStatus: " + status);
              });
    } 
</script>
<style type="text/css">
</style>
<input type="hidden" name="username" value="1">
<span style="display:none">var score = ${score };</span>
</head>

<body onclick="choujiang">
    <div align="center">
        <img width="300px" height="300px" src="log.jpg">
    </div>
    <div align="center">
        <p align="center">
        <table width="70%" align="right">
            <tr align="left">
                <td width="100%"><samp style="color: red; font-size: 34.0000pt">謝謝參加本次活動</samp><br />
                    <br /> <samp style="font-size: 40.0000pt">您本次總分數:</samp><br /> <br /></td>
            </tr>
        </table>
        <table width="100%" align="right">
            <tr align="left">
                <td width="100%">
                    <p align="center">
                        <samp style="font-size: 50.0000pt; color: red;">${score }</samp>
                    </p>

                </td>
            </tr>
        </table>
        <table width="70%" align="right">
            <tr align="left">
                <td width="100%"><samp style="font-size: 40.0000pt">如下是您的得分狀況</samp><br />
                    <br /></td>
            </tr>
        </table>
        <table width="100%" align="center">

            <c:forEach var="item" items="${list}" varStatus="i">
                <tr align="left">
                    <c:if test="${i.index ==0}">
                        <td align="left"><samp style="font-size: 30.0000pt;">${item}</samp><br />
                        </td>
                    </c:if>
                </tr>
                <tr align="left">
                    <c:if test="${i.index ==1}">
                        <td align="left"><samp style="font-size: 30.0000pt;">${item}</samp><br />
                        </td>
                    </c:if>
                </tr>
                <tr align="left">
                    <c:if test="${i.index ==2}">
                        <td align="left"><samp style="font-size: 30.0000pt;">${item}</samp><br />
                        </td>
                    </c:if>
                </tr>
                <tr align="left">
                    <c:if test="${i.index ==3}">
                        <td align="left"><samp style="font-size: 30.0000pt;">${item}</samp><br />
                        </td>
                    </c:if>
                </tr>
                <c:if test="${i.index ==4}">
                    <td align="left"><samp style="font-size: 30.0000pt;">${item}</samp><br /></td>
                </c:if>
                <tr align="left">
                    <c:if test="${i.index ==5}">
                        <td align="left"><samp style="font-size: 50.0000pt">${item}</samp><br />
                        </td>
                    </c:if>
                </tr>
            </c:forEach>


        </table>
    </div>
    </p>

    </div>
    <div align="center">
        <div id = "msg" name="msg" style="font-size:50.0000pt"></div>
        <samp style="color: red; font-size: 34.0000pt">請輸入中獎金額充值手機號:</samp>
        <input style="height:50px;width:800px;font-size:24.0000pt" type="text" id="phone" name="phone" onblur="msg();">
        <br><br>
        
    <input style="width:400px;height:100px;background: #EE4000;font-size:30px;" type="button" onclick="sbm()" value="點擊抽獎"></div>
    <script type="text/javascript">
        function sbm() {
            
            var num = $("#phone").val();
            if(num == null || $.trim(num) == "" || !/^((1)+\d{10})$/.test(num)){
                $("#msg").html("手機號碼格式不正確");
                return ;
            }else{
                $("#msg").html("");
            }
            
            if($("#msg").html()==""){
                window.location.href="index1.html?phone="+$("#phone").val();
            }
        }
    
        function msg() {
            //驗證手機號碼格式
            var num = $("#phone").val();
            if(num == null || $.trim(num) == "" || !/^((1)+\d{10})$/.test(num)){
                $("#msg").html("手機號碼格式不正確");
                return;
            }else{
                $("#msg").html("");
            }
        }
        
    </script>
</body>
</html>

點擊抽獎後到Index1.html:正則表達式

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>話費抽獎</title>
        <meta name="keywords" content="js大轉盤抽獎, 微信遊戲特效, css3動畫, 網頁特效" />
        <link href="css/style.css" rel="stylesheet" type="text/css">
            <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
            <script type="text/javascript" src="js/awardRotate.js"></script>

            <script type="text/javascript">
            function onBridgeReady() {
                WeixinJSBridge.call('hideOptionMenu');
            }

            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
                            false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
            var percent;
            var perc;
            var money;
            var item;
var turnplate={
        restaraunts:[],                //大轉盤獎品名稱
        colors:[],                    //大轉盤獎品區塊對應背景顏色
        outsideRadius:192,            //大轉盤外圓的半徑
        textRadius:155,                //大轉盤獎品位置距離圓心的距離
        insideRadius:68,            //大轉盤內圓的半徑
        startAngle:0,                //開始角度
        bRotate:false                //false:中止;ture:旋轉
};

$(document).ready(function(){
    //動態添加大轉盤的獎品與獎品區域背景顏色
    turnplate.restaraunts = ["50元話費", "20元話費", "10元話費", "5元話費", "感謝參與"];
    turnplate.colors = ["#FFF4D6", "#FFF4D7", "#FFF4D8", "#FFF4D9","#FFF4D10"];
    var reg = new RegExp("(^|&)"+ 'phone' +"=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象
    var r = window.location.search.substr(1).match(reg);  //匹配目標參數
    var phone = unescape(r[2]);


    var rotateTimeOut = function (){
        $('#wheelcanvas').rotate({
            angle:0,
            animateTo:2160,
            duration:8000,
            callback:function (){
                alert('網絡超時,請檢查您的網絡設置!');
            }
        });
    };

    //旋轉轉盤 item:獎品位置; txt:提示語;
    var rotateFn = function (item, txt){
        var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2));
        if(angles<270){
            angles = 270 - angles; 
        }else{
            angles = 360 - angles + 270;
        }
        $('#wheelcanvas').stopRotate();
        $('#wheelcanvas').rotate({
            angle:0,
            animateTo:angles+1800,
            duration:8000,
            callback:function (){
                alert(txt);
                turnplate.bRotate = !turnplate.bRotate;
            }
        });
    };

    $('.pointer').click(function (){
        if(turnplate.bRotate)return;
        turnplate.bRotate = !turnplate.bRotate;
        
        
        //獲取隨機數(獎品個數範圍內)
        //getPercent;
        item = rnd(1,2000);
          
        console.log(item);
        
          if(item ==1){
             money=50;
             item=1;
         }else if(item >5 && item<11){
             money=20;
             item=2;
         }else if(item >99 && item<110){
             money=10;
             item=3;
         }else if(item >200 && item<281){
             money=5;
             item=4;
         }else{
             money="未中獎";
             item=5;
         } 

          $.post("GetPrice.do",
          {
            
            
          },
          function(data,status){
            if(data!=1){
                alert("您已經抽過獎!")
                return;
            }else{
                if(money!=0){ 
                    $.post("GetPercent.do",
            {
            
                price:money
              },
              function(data){
                console.log("Data: " + data);
                if(data<=0){
                    money=0;
                    item=5;
                    //獎品數量等於10,指針落在對應獎品區域的中心角度[252, 216, 180, 144, 108, 72, 36, 360, 324, 288]
                    rotateFn(item, turnplate.restaraunts[item-1]);
                }
              //獎品數量等於10,指針落在對應獎品區域的中心角度[252, 216, 180, 144, 108, 72, 36, 360, 324, 288]
                rotateFn(item, turnplate.restaraunts[item-1]);
                
                
                
                
                      $.post("UserPrize.do",
                      {
                          price:money,
                            phone:phone
                      },
                      function(data,status){
                        console.log("Data: " + data + "\nStatus: " + status);
                      });
                    
              });
        
     }
                //獎品數量等於10,指針落在對應獎品區域的中心角度[252, 216, 180, 144, 108, 72, 36, 360, 324, 288]
                rotateFn(item, turnplate.restaraunts[item-1]);
            }
          });
        
          
          
               
            
             

        $("button").click(function(){
              $.post("demo_test_post.asp",
              {
                name:"Donald Duck",
                city:"Duckburg"
              },
              function(data,status){
                alert("Data: " + data + "\nStatus: " + status);
              });
            }); 
        
        console.log(item);
    });
});

function rnd(n, m){
    var random = Math.floor(Math.random()*(m-n+1)+n);
    return random;
    
}



//頁面全部元素加載完畢後執行drawRouletteWheel()方法對轉盤進行渲染
window.onload=function(){
    drawRouletteWheel();
};

function drawRouletteWheel() {    
  var canvas = document.getElementById("wheelcanvas");    
  if (canvas.getContext) {
      //根據獎品個數計算圓周角度
      var arc = Math.PI / (turnplate.restaraunts.length/2);
      var ctx = canvas.getContext("2d");
      //在給定矩形內清空一個矩形
      ctx.clearRect(0,0,422,422);
      //strokeStyle 屬性設置或返回用於筆觸的顏色、漸變或模式  
      ctx.strokeStyle = "#FFBE04";
      //font 屬性設置或返回畫布上文本內容的當前字體屬性
      ctx.font = '16px Microsoft YaHei';      
      for(var i = 0; i < turnplate.restaraunts.length; i++) {       
          var angle = turnplate.startAngle + i * arc;
          ctx.fillStyle = turnplate.colors[i];
          ctx.beginPath();
          //arc(x,y,r,起始角,結束角,繪製方向) 方法建立弧/曲線(用於建立圓或部分圓)    
          ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);    
          ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
          ctx.stroke();  
          ctx.fill();
          //鎖畫布(爲了保存以前的畫布狀態)
          ctx.save();   
          
          //----繪製獎品開始----
          ctx.fillStyle = "#E5302F";
          var text = turnplate.restaraunts[i];
          var line_height = 17;
          //translate方法從新映射畫布上的 (0,0) 位置
          ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);
          
          //rotate方法旋轉當前的繪圖
          ctx.rotate(angle + arc / 2 + Math.PI / 2);
          
          /** 下面代碼根據獎品類型、獎品名稱長度渲染不一樣效果,如字體、顏色、圖片效果。(具體根據實際狀況改變) **/
          if(text.indexOf("M")>0){//流量包
              var texts = text.split("M");
              for(var j = 0; j<texts.length; j++){
                  ctx.font = j == 0?'bold 20px Microsoft YaHei':'16px Microsoft YaHei';
                  if(j == 0){
                      ctx.fillText(texts[j]+"M", -ctx.measureText(texts[j]+"M").width / 2, j * line_height);
                  }else{
                      ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                  }
              }
          }else if(text.indexOf("M") == -1 && text.length>6){//獎品名稱長度超過必定範圍 
              text = text.substring(0,6)+"||"+text.substring(6);
              var texts = text.split("||");
              for(var j = 0; j<texts.length; j++){
                  ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
              }
          }else{
              //在畫布上繪製填色的文本。文本的默認顏色是黑色
              //measureText()方法返回包含一個對象,該對象包含以像素計的指定字體寬度
              ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
          }
          
          //添加對應圖標
          if(text.indexOf("閃幣")>0){
              var img= document.getElementById("shan-img");
              img.onload=function(){  
                  ctx.drawImage(img,-15,10);      
              }; 
              ctx.drawImage(img,-15,10);  
          }else if(text.indexOf("謝謝參與")>=0){
              var img= document.getElementById("sorry-img");
              img.onload=function(){  
                  ctx.drawImage(img,-15,10);      
              };  
              ctx.drawImage(img,-15,10);  
          }
          //把當前畫布返回(調整)到上一個save()狀態以前 
          ctx.restore();
          //----繪製獎品結束----
      }     
  } 
}

</script>
</head>
<body class="keBody">
    <span style="display: none">var score = ${score }</span>
    <h1 class="keTitle">話費抽獎</h1>
    <div class="kePublic">
        <!--效果html開始-->
        <div style="max-width: 800px; margin: 0 auto">
            <img src="images/1.png" id="shan-img" style="display: none;" /> <img
                src="images/2.png" id="sorry-img" style="display: none;" />
            <div class="banner">
                <div class="turnplate"
                    style="background-image: url(images/turnplate-bg.png); background-size: 100% 100%;">
                    <canvas class="item" id="wheelcanvas" width="422px" height="422px"></canvas>
                    <img class="pointer" src="images/turnplate-pointer.png" />
                </div>
            </div>
        </div>
</body>
</html>

UserPrize:sql

package com.bt.action;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

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

import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserPrize extends HttpServlet{
    private  final String URL = "jdbc:mysql://120.77.200.159:3306/fzbl";
    private  final String UNAME = "root";
    private  final String PWD = "RM+jt@#201710";
    private Connection conn = null;
    public  UserPrize() {
        try{
        Class.forName("com.mysql.jdbc.Driver");
        // 2.������ݿ������
         conn = DriverManager.getConnection(URL, UNAME, PWD);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    @SuppressWarnings("static-access")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = (String) request.getSession().getAttribute("username");
        String price=(String)request.getParameter("price");
        String phone=request.getParameter("phone");
        PreparedStatement ptmt;
        PreparedStatement ptmt1;
        
        try {
            String sql = "update fzbl.s_user set userPrize = ?,phone=? where userName = ?";
            ptmt = conn.prepareStatement(sql);
            ptmt.setString(3, userName);
            ptmt.setString(1, price);
            ptmt.setString(2, phone);
            ptmt.execute();
            ptmt.close();
            if(price!="未中獎") {
                String sql1 = "update prize set number = number-1 where price = ?";
                ptmt1 = conn.prepareStatement(sql1);
                ptmt1.setString(1, price);
                ptmt1.execute();
                ptmt1.close();
            }else {
                String sql1 = "update prize set number = number-1 where price = ?";
                ptmt1 = conn.prepareStatement(sql1);
                ptmt1.setString(1, "weizhongjiang");
                ptmt1.execute();
                ptmt1.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

UserScore:

package com.bt.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserScore {
    private final String URL = "jdbc:mysql://120.77.200.159:3306/fzbl";
    private final String UNAME = "root";
    private final String PWD = "RM+jt@#201710";
    private Connection conn = null;

    public UserScore() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.������ݿ������
            conn = DriverManager.getConnection(URL, UNAME, PWD);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    @SuppressWarnings("static-access")
    public boolean doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = (String) request.getSession().getAttribute("username");
        PreparedStatement ptmt;
        String sql1 = "select * from fzbl.s_user where userName = ?";
        try {
            ptmt = conn.prepareStatement(sql1);
            ptmt.setString(1, userName);
            ResultSet result = ptmt.executeQuery();
            if (result.next() != false) {
                ptmt.close();
                return false;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int score = (int) request.getAttribute("score");
        String sql = "insert into s_user(userName,score) values(?,?)";

        try {
            ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, userName);
            ptmt.setInt(2, score);
            ptmt.execute();
            ptmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }
}

GetPrice:

package com.bt.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class GetPrice extends HttpServlet{
    private final String URL = "jdbc:mysql://120.77.200.159:3306/fzbl";
    private final String UNAME = "root";
    private final String PWD = "RM+jt@#201710";
    private Connection conn = null;

    public GetPrice() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.������ݿ������
            conn = DriverManager.getConnection(URL, UNAME, PWD);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     * @throws SQLException 
     */
    @SuppressWarnings("static-access")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = (String) request.getSession().getAttribute("username");
        PreparedStatement ptmt;
        String sql1 = "select userPrize from fzbl.s_user where userName = ?";
        try {
            ptmt = conn.prepareStatement(sql1);
            ptmt.setString(1, userName);
            ResultSet result = ptmt.executeQuery();
            if (result.next() != false) {
                Object int1 = result.getObject("userPrize");
                if(int1==null) {
                    int1="1";
                }
                
                PrintWriter out = response.getWriter();
                System.err.println(int1);
                out.println(int1);
                ptmt.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        
    
}
package com.bt.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class GetPercent extends HttpServlet{
    private final String URL = "jdbc:mysql://120.77.200.159:3306/fzbl";
    private final String UNAME = "root";
    private final String PWD = "RM+jt@#201710";
    private Connection conn = null;

    public GetPercent() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.������ݿ������
            conn = DriverManager.getConnection(URL, UNAME, PWD);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     * @throws SQLException 
     */
    @SuppressWarnings("static-access")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        String price=request.getParameter("price");
        PreparedStatement ptmt2;
        String sql2 = "select number from prize where price = ?";
        try {
            ptmt2 = conn.prepareStatement(sql2);
            ptmt2.setString(1, price);
            ResultSet result = ptmt2.executeQuery();
            while(result.next())
            {
                response.getWriter().print(result.getInt("number"));
                }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }
        
    
}
相關文章
相關標籤/搜索