📄 members.java
字号:
"E-Mail: " + email + "<br/>" + "Phone: " + phone + "<br/>"); out.print("Push-Services: "); StringTokenizer st = new StringTokenizer(pushservices, ";"); while (st.hasMoreTokens()) out.println(((String[])pushServices.get(Integer.parseInt(st.nextToken())))[0]); out.println(" </p>" + " <do type=\"accept\" label=\"Ok\">" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" + " </do>" + "</card>"); } // Delete member information from the database. // "action" has the value "delete". else if(action.equals("delete")) { int nDeletedRows = 0; try { String sqlDeleteStmt = "DELETE FROM members WHERE name = \'" + name + "\' AND passwd = \'" + passwd + "\'"; nDeletedRows = stmt.executeUpdate(sqlDeleteStmt); session.removeAttribute("name"); } catch (SQLException E){ E.printStackTrace(); } out.println("<card id=\"members\">\n<p>" + ((nDeletedRows > 0) ? "Your (" + name + ") membership is now ended, and member information cleared from the database" : "Could not remove member (" + name + ")") + " <do type=\"accept\" label=\"Ok\">" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" + " </do>" + "</p></card>"); } /* bind the member name and other parameters (stored in database) to the current session */ else if(action.equals("checkin")) { boolean access = false; if (name != null && passwd != null) { try { ResultSet rs = stmt.executeQuery("SELECT name, passwd, dob, sex, email, phone, pushservices FROM members"); while (rs.next()) { String nameCheck = rs.getString("name"); String passwdCheck = rs.getString("passwd"); if (nameCheck.equals(name) && passwdCheck.equals(passwd)){ access = true; setSessionInfo(session, name, passwd, rs.getString("dob"), rs.getString("sex"), rs.getString("email"), rs.getString("phone"), rs.getString("pushservices")); break; } } } catch (SQLException E) { E.printStackTrace(); } } if (access) { out.println("<card id=\"members\" title=\"Members\" ontimer=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\">"); out.println("<timer value=\"30\"/>" + "<p align=\"center\">Welcome<br/><strong>" + name + "</strong><br/> to members' site!<br/>"); if(isPushSupported) { out.println("A push message is coming..."); /* next, send a welcome push message */ String serviceUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/services/news.jsp"; String message = PushInitiator.createSiMessage(serviceUrl, null, null, null, null, "Thank you for vising our zoo! Check the latest news and happenings"); String address = pushAddressType.equals("IPv4") ? request.getRemoteAddr() : getPhoneNumber(name); try { pusher.sendPushMessage(address, pushAddressType, message, PushService.SI_CONTENT_TYPE); } catch(Exception ee) {} } out.println("</p></card>"); } else { out.println("<card title=\"Name not found\" id=\"notfound\">" + " <p>Please check username/password.<br/>" + " <do type=\"accept\" label=\"Ok\">" + " <go href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"login\"/>" + " </go>" + " </do>" + " <anchor title=\"Try again\">Try again" + " <go href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"login\"/>" + " </go>" + " </anchor>" + " </p>" + "</card>"); } } /* insert a new member information into database */ else if (action.equals("insert")) { boolean insertOk=false; boolean nameAlreadyExists=false; if(name != null && !name.equals("")) { // first, check if the name already exists! try { ResultSet rs = stmt.executeQuery("SELECT * FROM members WHERE name = \'" + name + "\'"); if(!rs.next()) { int res = stmt.executeUpdate("INSERT INTO members VALUES ('" + name + "','" + passwd + "','" + dob + "','" + sex + "','" + email + "','" + phone + "','" + pushservices + "')"); if(res == 1) insertOk = true; } else nameAlreadyExists=true; } catch (SQLException E){ E.printStackTrace(); } if(insertOk) setSessionInfo(session, name, passwd, dob, sex, email, phone, pushservices); } out.println("<card id=\"newmember\" title=\"New member\">" + " <p><strong>" + name + ":</strong> " + (insertOk ? "You are now a member of Mobile Zoo!" : (nameAlreadyExists ? "Username " + name + " already exists in the database. Please, select an other username" : "Missing information? The user information is not added to the database!?")) + "<br/>" + " <anchor title=\"Ok\">Ok" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" + " </anchor>" + " </p>" + " <do type=\"accept\" label=\"Ok\">" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" + " </do>" + "</card>"); } /* update member information */ else if(action.equals("update")) { try { String sqlUpdateStmt = "UPDATE members SET passwd = \'" + passwd + "\', dob = \'" + dob + "\', sex = \'" + sex + "\', email = \'" + email + "\', phone = \'" + phone + "\', pushservices = \'" + pushservices + "\' WHERE name = \'" + name + "\'"; stmt.executeUpdate(sqlUpdateStmt); } catch (SQLException E){ E.printStackTrace(); } setSessionInfo(session, name, passwd, dob, sex, email, phone, pushservices); out.println("<card id=\"members\" title=\"Members\">" + " <p align=\"center\">Member information of  <strong>" + name + "</strong>  has been updated!<br/>" + " <anchor title=\"Ok\">Ok" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID +"\"/>" + " </anchor>" + " </p>" + " <do type=\"accept\" label=\"Ok\">" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID +"\"/>" + " </do>" + "</card>"); } /* new member login or changing information */ else if(action.equals("new") || action.equals("change")) { out.println("<card title=\"New member\" id=\"newmember\">"); out.print("<p>Login name: "); if(action.equals("change")) out.println("<em>" + name + "</em><br/>"); else if(name != null) out.println("<input name=\"name\" value=\"" + name + "\"/>"); else out.println("<input name=\"name\"/>"); out.println("Password: <input type=\"password\" name=\"passwd\"" + ((passwd != null) ? " value=\"" + passwd + "\"" : "") + "/>" + "Re-enter password: <input type=\"password\" name=\"rpasswd\"/>" + "Date of birth (yyyy.mm.dd): <input format=\"NNNN\\.NN\\.NN\" name=\"dob\"" + ((dob != null) ? " value=\"" + dob + "\"" : "") + "/>" + "E-Mail: <input name=\"email\"" + ((email != null) ? " value=\"" + email + "\"" : "") + "/>" + "Phone: <input format = \"*N\" name=\"phone\"" + ((phone != null) ? " value=\"" + phone + "\"" : "") + "/>" + "Sex: " + "<select name=\"sex\"" + ((sex != null) ? " value=\"" + sex + "\"" : "") + ">" + " <option value=\"m\">Male</option>" + " <option value=\"f\">Female</option>" + "</select>" + "Push-Services: " + "<select name=\"pushservices\" title=\"Push Services\" multiple=\"true\"" + ((pushservices != null) ? " value=\"" + pushservices + "\"" : "") +">"); for (int i = 0; i < pushServices.size(); ++i){ out.println("<option value=\"" + i + "\">" + ((String[])pushServices.get(i))[0] + "</option>"); } out.println("</select>" + "<anchor title=\"Ok\">Ok" + " <go href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"name\" value=\"$(name)\"/>" + " <postfield name=\"passwd\" value=\"$(passwd)\"/>" + " <postfield name=\"rpasswd\" value=\"$(rpasswd)\"/>" + " <postfield name=\"dob\" value=\"$(dob)\"/>" + " <postfield name=\"sex\" value=\"$(sex)\"/>" + " <postfield name=\"email\" value=\"$(email)\"/>" + " <postfield name=\"phone\" value=\"$(phone)\"/>" + " <postfield name=\"pushservices\" value=\"$(pushservices)\"/>" + " <postfield name=\"action\" value=\"" + (action.equals("new") ? "insert" : "update") + "\"/>" + " </go>" + "</anchor>" + "</p>" + "<do type=\"accept\" label=\"Ok\" name=\"1\">" + " <go href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"name\" value=\"$(name)\"/>" + " <postfield name=\"passwd\" value=\"$(passwd)\"/>" + " <postfield name=\"rpasswd\" value=\"$(rpasswd)\"/>" + " <postfield name=\"dob\" value=\"$(dob)\"/>" + " <postfield name=\"sex\" value=\"$(sex)\"/>" + " <postfield name=\"email\" value=\"$(email)\"/>" + " <postfield name=\"phone\" value=\"$(phone)\"/>" + " <postfield name=\"pushservices\" value=\"$(pushservices)\"/>" + " <postfield name=\"action\" value=\"" + (action.equals("new") ? "insert" : "update") + "\"/>" + " </go>" + "</do>" + "<do type=\"accept\" label=\"Quit\" name=\"2\">" + " <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" + "</do>" + "</card>"); } out.println("</wml>"); } /** * <code>setSessionInfo</code> relates attributes to session * * @param s a <code>HttpSession</code> value * @param name a <code>String</code> value * @param passwd a <code>String</code> value * @param dob a <code>String</code> value * @param sex a <code>String</code> value * @param email a <code>String</code> value * @param phone a <code>String</code> value * @param pushservices a <code>String</code> value */ private void setSessionInfo(HttpSession s, String name, String passwd, String dob, String sex, String email, String phone, String pushservices) { s.setMaxInactiveInterval(600); // 10 min s.setAttribute("name", name); s.setAttribute("passwd", passwd); s.setAttribute("dob", dob); s.setAttribute("sex", sex); s.setAttribute("email", email); s.setAttribute("phone", phone); s.setAttribute("pushservices", pushservices); } /** * Check if the client phone supports Push functionality. * The user-agent header information is utilised. * * @param request a <code>HttpServletRequest</code> value * @return a <code>boolean</code> value */ private static boolean pushSupported(HttpServletRequest request) { String userAgent = request.getHeader("User-Agent"); if(userAgent == null) return false; userAgent = userAgent.toLowerCase(); try { BufferedReader reader = new BufferedReader(new FileReader(pushSupportFile)); String line; while((line = reader.readLine()) != null) { line = line.trim().toLowerCase(); if(line.equals("")) break; if(userAgent.startsWith(line)) return true; } } catch (java.io.IOException e) { System.err.println(e); } return false; } /** * Finds the user's phone number from the database * * @param user a <code>String</code> value * @return a <code>String</code> value */ public static String getPhoneNumber(String user) { try { ResultSet rs = stmt.executeQuery("SELECT phone FROM members WHERE name = \'" + user + "\'"); return rs.next() ? rs.getString("phone") : null; } catch (SQLException e) { System.err.println(e); } return null; } /** * <code>getInitiator</code> returns the instance of the default push initiator. * * @return a <code>PushInitiator</code> value */ public static PushInitiator getInitiator() { return pusher; } /** * <code>getPushService</code> * * @param id an <code>int</code> value * @return an <code>Object</code> value */ public static Object getPushService(int id) { return pushServices.get(id); } /** * <code>getStatement</code> * * @return a <code>Statement</code> value */ public static Statement getStatement() { return stmt; } /** * Returns the push address type (IPv4 or PLMN) to be used * * @return a <code>String</code> value */ public static String getPushAddressType() { return pushAddressType; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -