繼上面第二個(gè)springboot項(xiàng)目后,今晚應(yīng)朋友需求幫忙寫個(gè)抽獎(jiǎng)程序,想著就用springboot+servlet去發(fā)布一個(gè)服務(wù)給前端直接調(diào)用或者后端直接調(diào)用。
下面來介紹一下:
1、不錯(cuò),依然需要一個(gè)Application的啟動(dòng)springboot的入口
[java]
view plain copyprint?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);
}
}
特別的注意一下,是通過使用注解注冊(cè)Servlet
@ServletComponentScan
2、下面就創(chuàng)建一個(gè)類來實(shí)現(xiàn)HttpServlet[java]
view plain copyprint?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 = "請(qǐng)?jiān)谇岸薬jax調(diào)用或者直接httpclient直接調(diào)用,可以返回抽獎(jiǎng)結(jié)果,需要?jiǎng)e的功能可以繼續(xù)擴(kuò)展")
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[][]{
//獎(jiǎng)品id,min,max,prize【獎(jiǎng)項(xiàng)】,v【中獎(jiǎng)率】
{1,1,14,"一等獎(jiǎng)",1},
{2,346,364,"一等獎(jiǎng)",1},
{3,16,44,"不要灰心",10},
{4,46,74,"神馬也沒有",10},
{5,76,104,"祝您好運(yùn)",10},
{6,106,134,"二等獎(jiǎng)",2},
{7,136,164,"再接再厲",10},
{8,166,194,"神馬也沒有",10},
{9,196,224,"運(yùn)氣先攢著",10},
{10,226,254,"三等獎(jiǎng)",5},
{11,256,284,"要加油哦",10},
{12,286,314,"神馬也沒有",10},
{13,316,344,"謝謝參與",10}
};
Object result[] = award(prizeArr);//抽獎(jiǎng)后返回角度和獎(jiǎng)品等級(jí)
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("{\"angle\":\""+result[0]+"\",\"msg\":\""+result[2]+"\"}");
System.out.println("轉(zhuǎn)動(dòng)角度:"+result[0]+"\t獎(jiǎng)項(xiàng)ID:"+result[1]+"\t提示信息:"+result[2]);
}
//抽獎(jiǎng)并返回角度和獎(jiǎng)項(xiàng)
public Object[] award(Object[][] prizeArr){
//概率數(shù)組
Integer obj[] = new Integer[prizeArr.length];
for(int i=0;i<prizeArr.length;i++){
obj[i] = (Integer) prizeArr[i][4];
}
Integer prizeId = getRand(obj); //根據(jù)概率獲取獎(jiǎng)項(xiàng)id
//旋轉(zhuǎn)角度
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};
}
//根據(jù)概率獲取獎(jiǎng)項(xiàng)
public Integer getRand(Integer obj[]){
Integer result = null;
try {
int sum = 0;//概率數(shù)組的總概率精度
for(int i=0;i<obj.length;i++){
sum+=obj[i];
}
for(int i=0;i<obj.length;i++){//概率數(shù)組循環(huán)
int randomNum = new Random().nextInt(sum);//隨機(jī)生成1到sum的整數(shù)
if(randomNum<obj[i]){//中獎(jiǎng)
result = i;
break;
}else{
sum -=obj[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
3、運(yùn)行Application的main之后,直接瀏覽器訪問 http://127.0.0.1:8080/lottery/
Go 即可得到抽獎(jiǎng)結(jié)果