小program Development Tutorial"
GitHub address: Portal
Here is the key code:
/** * Servlet implementation class Login */@WebServlet("/Login")public class Login extends HttpServlet { private static final long serialVersionUID = 1L; private static final String APPID="xxxxxxxxxx"; private static final String SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxx"; /** * Default constructor. */ public Login() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //ÉèÖÃÇëÇó±àÂë request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); /* ÉèÖÃÏìӦͷÔÊÐíajax¿çÓò·ÃÎÊ */ response.setHeader("Access-Control-Allow-Origin", "*"); /* ÐǺűíʾËùÓеÄÒìÓòÇëÇ󶼿ÉÒÔ½ÓÊÜ£¬ */ response.setHeader("Access-Control-Allow-Methods", "GET,POST"); String flag=request.getParameter("flag"); // System.out.println(flag); if("login".equals(flag)) { String code=request.getParameter("js_code"); String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+ "&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code"; JSONObject sjson=CommonUtil.httpsRequest(url, "GET", null); /*String openid=""; String session_key=""; if (sjson != null) { try { openid = sjson.getString("openid"); session_key=sjson.getString("session_key"); } catch (Exception e) { System.out.println("ÒµÎñ²Ù×÷ʧ°Ü"); e.printStackTrace(); } } else { System.out.println("codeÎÞЧ"); } System.out.println(session_key+" "+openid);*/ /*Map<String, Object> result = new HashMap<String, Object>(); result.put("res", "test"); result.put("msg", "ºǫ́ÒÑÊÕµ½");*/ // String json = new Gson().toJson(sjson); // System.out.println(json); Writer out=response.getWriter(); out.write(sjson.toString()); out.flush(); } if("init".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); boolean res=true; try { res=studentDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } Map<String, Object> result = new HashMap<String, Object>(); result.put("res", res); result.put("msg", "ºǫ́ÒÑÊÕµ½"); String json = new Gson().toJson(result); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } if("student".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); String studentName=request.getParameter("sname"); String studentNum=request.getParameter("snum"); Student student=new Student(userid, studentName, studentNum,new Date()); try { int a=studentDAO.create(student); if(a!=0) { System.out.println("²åÈë³É¹¦"); } } catch (Exception e) { e.printStackTrace(); } } if("teacher".equals(flag)) { TeacherDAO teacherDAO=new TeacherDAO(); String userid=request.getParameter("userid"); String teacherName=request.getParameter("tname"); String teacherID=request.getParameter("tnum"); Teacher tea=new Teacher(userid, teacherID, teacherName,new Date()); try { int a=teacherDAO.create(tea); if(a!=0) { System.out.println("²åÈë³É¹¦"); } } catch (Exception e) { e.printStackTrace(); } } if("guide".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); System.out.println(userid); boolean res=true; String state=""; try { res=studentDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } if(res) { state="student"; } else{ TeacherDAO teacherDAO=new TeacherDAO(); try { res=teacherDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } if(res) { state="teacher"; } else { state="none"; } } String json = new Gson().toJson(state); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } if("myInfo".equals(flag)) { String userid=request.getParameter("userid"); StudentDAO studentDAO=new StudentDAO(); try { List<String> list=studentDAO.myInfo(userid); Map<String, String> result = new HashMap<String, String>(); result.put("backName",list.get(0)); result.put("backNum", list.get(1)); String json = new Gson().toJson(result); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } catch (Exception e) { e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
The above is the detailed content of Share the WeChat applet check-in and attendance back-end code. For more information, please follow other related articles on the PHP Chinese website!