Following the second Springboot project, tonight, a friend needs help to write a lottery program, thinking about using Springboot+servlet to publish a service to the front-end direct calls or back-end direct calls.
Here's a look at:
1, good, still need a application start springboot of the entrance
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 @servletcomponentscanpublic class Springbootservletsampleapplication {public static void Main (string[] args) { springapplication.run (Springbootservletsampleapplication.class,args); }}
In particular, note that the servlet is registered by using annotations
@ServletComponentScan
2. Create a class below to implement 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 2016/ 5/7. */@WebServlet (urlpatterns = "/lottery/go", description = "Please call directly on the front-end Ajax call or directly httpclient, you can return the lottery results, need other functions can continue to expand") public class Lotteryservlet extends httpservlet{@Override protected void doget (HttpServletRequest req, httpservletr Esponse resp) throws Servletexception, IOException {this.dopost (REQ,RESP); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexcep tion {object[][] Prizearr = new object[][]{//Prizes Id,min,max,prize "awards", V "winning rate" {1,1,14 , "First Prize", 1}, {2,346,364, "First Prize", 1}, {3,16,44, "Do not Lose Heart", 10}, {4,46,74, "Neither God nor horse", 10}, {5, 76,104,"Good luck", 10}, {6,106,134, "Second Prize", 2}, {7,136,164, "persistent", 10}, {8,166,194, "No Gods and Horses", 10 }, {9,196,224, "fortune First", 10}, {10,226,254, "Third Prize", 5}, {11,256,284, "to refuel Oh", 10}, {12,286,314, "God horse does not have", 10}, {13,316,344, "Thank you for participating", 10}}; Object result[] = Award (Prizearr);//return angle and prize level Resp.setcontenttype ("Text/html;charset=utf-8") after the draw; Resp.getwriter (). Write ("{\" angle\ ": \" "+result[0]+" \ ", \" msg\ ": \" "+result[2]+" \ "}"); System.out.println ("Rotation angle:" +result[0]+ "\ T Prize ID:" +result[1]+ "\ t prompt message:" +result[2]); }//Draw and return to angle and awards public object[] Award (object[][] Prizearr) {//probability array Integer obj[] = new Integer[prizearr . length]; for (int i=0;i<prizearr.length;i++) {Obj[i] = (Integer) prizearr[i][4]; } Integer Prizeid = Getrand (obj); Get the prize ID//rotation angle int angle = new Random () based on probability. Nextint ((integer) prizearr[prizeid][2]-(integer) PrizEARR[PRIZEID][1]) + (Integer) prizearr[prizeid][1]; String msg = (string) prizearr[prizeid][3];//prompt message return new object[]{angle,prizeid,msg}; }//Get prize according to probability public integer getrand (integer obj[]) {integer result = null; try {int sum = 0;//probability array's total probability precision for (int i=0;i<obj.length;i++) {sum+=obj[i]; } for (int i=0;i<obj.length;i++) {//probability array loop int randomnum = new Random (). Nextint (sum);//random Generates an integer of 1 to sum if (Randomnum<obj[i]) {//winning result = i; Break }else{sum-=obj[i]; }}} catch (Exception e) {e.printstacktrace (); } return result; }}
3, after running application main, direct browser access to Http://127.0.0.1:8080/lottery/go can get lottery results
My third Springboot project, Servlet implements a carousel lottery program