码迷,mamicode.com
首页 > 编程语言 > 详细

我的第三个springboot项目,servlet实现一个转盘抽奖程序

时间:2016-05-13 03:27:22      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:

继上面第二个springboot项目后,今晚应朋友需求帮忙写个抽奖程序,想着就用springboot+servlet去发布一个服务给前端直接调用或者后端直接调用。

下面来介绍一下:

1、不错,依然需要一个Application的启动springboot的入口

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * Created by LK on 2016/5/7.
 */
@SpringBootApplication
@ServletComponentScan
public class SpringBootServletSampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootServletSampleApplication.class,args);
    }
}

特别的注意一下,是通过使用注解注册Servlet    

@ServletComponentScan


2、下面就创建一个类来实现HttpServlet

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

/**
 * Created by LK on 2016/5/7.
 */
@WebServlet(urlPatterns = "/lottery/go",description = "请在前端ajax调用或者直接httpclient直接调用,可以返回抽奖结果,需要别的功能可以继续扩展")
public class LotteryServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object[][] prizeArr = new  Object[][]{
                //奖品id,min,max,prize【奖项】,v【中奖率】
                {1,1,14,"一等奖",1},
                {2,346,364,"一等奖",1},
                {3,16,44,"不要灰心",10},
                {4,46,74,"神马也没有",10},
                {5,76,104,"祝您好运",10},
                {6,106,134,"二等奖",2},
                {7,136,164,"再接再厉",10},
                {8,166,194,"神马也没有",10},
                {9,196,224,"运气先攒着",10},
                {10,226,254,"三等奖",5},
                {11,256,284,"要加油哦",10},
                {12,286,314,"神马也没有",10},
                {13,316,344,"谢谢参与",10}
        };
        Object result[] = award(prizeArr);//抽奖后返回角度和奖品等级
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("{\"angle\":\""+result[0]+"\",\"msg\":\""+result[2]+"\"}");
        System.out.println("转动角度:"+result[0]+"\t奖项ID:"+result[1]+"\t提示信息:"+result[2]);
    }
    //抽奖并返回角度和奖项
    public Object[] award(Object[][] prizeArr){
        //概率数组
        Integer obj[] = new Integer[prizeArr.length];
        for(int i=0;i<prizeArr.length;i++){
            obj[i] = (Integer) prizeArr[i][4];
        }
        Integer prizeId = getRand(obj); //根据概率获取奖项id
        //旋转角度
        int angle = new Random().nextInt((Integer)prizeArr[prizeId][2]-(Integer)prizeArr[prizeId][1])+(Integer)prizeArr[prizeId][1];
        String msg = (String) prizeArr[prizeId][3];//提示信息
        return new Object[]{angle,prizeId,msg};
    }

    //根据概率获取奖项
    public Integer getRand(Integer obj[]){
        Integer result = null;
        try {
            int  sum = 0;//概率数组的总概率精度
            for(int i=0;i<obj.length;i++){
                sum+=obj[i];
            }
            for(int i=0;i<obj.length;i++){//概率数组循环
                int randomNum = new Random().nextInt(sum);//随机生成1到sum的整数
                if(randomNum<obj[i]){//中奖
                    result = i;
                    break;
                }else{
                    sum -=obj[i];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

3、运行Application的main之后,直接浏览器访问 http://127.0.0.1:8080/lottery/go  即可得到抽奖结果

技术分享

我的第三个springboot项目,servlet实现一个转盘抽奖程序

标签:

原文地址:http://blog.csdn.net/lk10207160511/article/details/51340564

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!