?? members.java
字號:
package members;import push.Birthday;import push.PushService;import push.PushInitiator;import push.User;import feedback.Feedback;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import java.sql.*;import java.net.InetAddress;import java.net.URL;/** * Members login * * @see HttpServlet */public final class Members extends HttpServlet { private static final String XML_VERSION = "<?xml version=\"1.0\"?>"; private static final String CONTENT_TYPE = "text/vnd.wap.wml"; private static final String DOC_TYPE = "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n" + " \"http://www.wapforum.org/DTD/wml_1.1.xml\">"; private static Connection con; private static Statement stmt = null; private static String membersFile = null; private static final String HOME_PAGE = "/index.jsp"; private static final String ADMIN_NAME = "admin"; /* push initiator */ private static PushInitiator pusher = null; /* push proxy gateway url */ private static String ppgUrl = null; private static String authorization = null; /* push services */ private static Vector pushServices = new Vector(); private static Birthday birthdayChecker = null; private static PushService pushServiceServer = null; private static String pushSupportFile = null; private static String pushAddressType = null; /** * This is the initialization code that runs only once. The servlet is being * placed into service. * * @exception ServletException * if an error occurs */ public void init() throws ServletException { String pushServiceStr = getInitParameter("pushservices"); StringTokenizer st = new StringTokenizer(pushServiceStr, ";"); String[] service; while (st.hasMoreTokens()) { try { service = new String[2]; service[0] = st.nextToken().trim(); service[1] = st.nextToken().trim(); pushServices.add(service); } catch (Exception e) { System.err.println(e); break; } } // check, if proxy is set boolean useProxy = new Boolean(getInitParameter("use-proxy")) .booleanValue(); if (useProxy) { // HTTP connections via a proxy server String proxyHost = getInitParameter("proxy-host"); String proxyPort = getInitParameter("proxy-port"); System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); } try { Class.forName(getInitParameter("dbdriver")); con = DriverManager.getConnection(getInitParameter("database"), getInitParameter("dbuser"), getInitParameter("dbuserpasswd")); stmt = con.createStatement(); try { stmt .executeUpdate("CREATE TABLE IF NOT EXISTS members (name VARCHAR(10) NOT NULL, passwd VARCHAR(10), dob VARCHAR(10), sex CHAR(1), email VARCHAR(30), phone VARCHAR(15), pushservices VARCHAR(20), PRIMARY KEY (name))"); } catch (SQLException e) { System.err.println(e); } } catch (Exception ex) { ex.printStackTrace(); } /* Create a push initiator, used to initiate push messages */ ppgUrl = getInitParameter("ppg-url"); /* Some ppgs require authorization */ authorization = getInitParameter("authorization"); pushSupportFile = getInitParameter("pushsupport"); pushSupportFile = getServletContext().getRealPath(pushSupportFile); /* Get push address type. It is either "IPv4" (default) or "PLMN". */ pushAddressType = getInitParameter("push-addresstype"); if (pushAddressType == null) pushAddressType = "IPv4"; } /** * Called by the server to allow a servlet to handle a GET request. * * @param request * a <code>HttpServletRequest</code> value * @param response * a <code>HttpServletResponse</code> value * @exception ServletException * if an error occurs * @exception IOException * if an error occurs */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * Called by the server to allow a servlet to handle a POST request. * * @param request * a <code>HttpServletRequest</code> value * @param response * a <code>HttpServletResponse</code> value * @exception ServletException * if an error occurs * @exception IOException * if an error occurs */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); boolean cookiesCheck = request.getHeader("cookie") != null && request.isRequestedSessionIdValid(); final String jsessionID = cookiesCheck ? "" : ";jsessionid=" + session.getId(); /* * Request parameter "action" has one of the following values: - "login" * - "new" - "checkin" - "change" - "delete" - "view" - "update" - * "insert" */ String action = request.getParameter("action"); if (action == null) action = "login"; // default action String name = null; String passwd = null; String dob = null; String sex = null; String email = null; String phone = null; String pushservices = null; if (action.equals("new") || action.equals("update") || action.equals("insert") || action.equals("checkin")) { name = action.equals("update") ? (String) session .getAttribute("name") : request.getParameter("name"); passwd = request.getParameter("passwd"); dob = request.getParameter("dob"); sex = request.getParameter("sex"); email = request.getParameter("email"); phone = request.getParameter("phone"); pushservices = request.getParameter("pushservices"); } else { // set values from the session attributes name = (String) session.getAttribute("name"); passwd = (String) session.getAttribute("passwd"); dob = (String) session.getAttribute("dob"); sex = (String) session.getAttribute("sex"); email = (String) session.getAttribute("email"); phone = (String) session.getAttribute("phone"); pushservices = (String) session.getAttribute("pushservices"); } // check if the client device supports WAP push */ boolean isPushSupported = pushSupported(request); // assing a push-info object into session information of the user */ if (name != null) { if (session.getAttribute("push-info") == null && isPushSupported) session.setAttribute("push-info", new User(name, request .getRemoteAddr())); } if (pusher == null) { try { URL contextRoot = new URL("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"); pusher = new PushInitiator(new URL(contextRoot, ppgUrl), authorization); } catch (Exception e) { System.err.println(e); } } /* * Create a members' birthday checker thread. A push message is sent * when the birthday of a member is approaching */ if (birthdayChecker == null) { birthdayChecker = new Birthday(pusher, "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + HOME_PAGE, pushAddressType); new Thread(birthdayChecker).start(); } /* * Create a push service server thread. The thread sends push messages * to users. A user will receive push messages if he/she has selected * any push services. */ if (pushServiceServer == null) { pushServiceServer = new PushService(pusher, "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(), pushAddressType, 24); new Thread(pushServiceServer).start(); } // start generating the response ... response.setContentType(CONTENT_TYPE); // prevent caching of the response response.setHeader("cache-control", "no-cache"); PrintWriter out = response.getWriter(); out.println(XML_VERSION); out.println(DOC_TYPE); out.println("<wml>"); out.println("<!-- provides a way back using the prev element -->\n" + "<template>" + " <do type=\"prev\">" + " <prev/>" + " </do>" + "</template>"); /* Handle the "login" action */ if (action.equals("login")) { if (name == null) { out .println("<card title=\"Members\" id=\"login1\">" + " <p>Name: <input name=\"name\"/>" + "Password: <input type=\"password\" name=\"passwd\"/><br/>" + " <anchor title=\"Ok\">Ok" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"name\" value=\"$(name)\"/>" + " <postfield name=\"passwd\" value=\"$(passwd)\"/>" + " <postfield name=\"action\" value=\"checkin\"/>" + " </go>" + " </anchor><br/>" + " <anchor title=\"New member\">New member" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"new\"/>" + " </go>" + " </anchor>" + " </p>" + " <do type=\"accept\" label=\"Ok\" name=\"1\">" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"name\" value=\"$(name)\"/>" + " <postfield name=\"passwd\" value=\"$(passwd)\"/>" + " <postfield name=\"action\" value=\"checkin\"/>" + " </go>" + " </do>" + " <do type=\"accept\" label=\"New member\" name=\"5\">" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"new\"/>" + " </go>" + " </do>" + "</card>"); } else { // name != null out .println("<card title=\"Members\" id=\"login\">" + " <p>Next weekend all members can enjoy a free snack at the Safari restaurant<br/>" + " <anchor title=\"My membership\">My membership" + " <go href=\"#membership\"/>" + " </anchor></p>" + "</card>"); out .println("<card title=\"My membership\" id=\"membership\">" + " <p>Member name:  <strong>" + name + "</strong><br/>" + " <anchor title=\"View\">View info" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"view\"/>" + " </go>" + " </anchor><br/>" + " <anchor title=\"Change\">Edit" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"change\"/>" + " </go>" + " </anchor><br/>" + " <anchor title=\"End membership\">End membership" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"delete\"/>" + " </go>" + " </anchor>" + " </p>" + " <do type=\"accept\" label=\"View info\" name=\"3\">" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"view\"/>" + " </go>" + " </do>" + " <do type=\"accept\" label=\"Edit\" name=\"4\">" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"change\"/>" + " </go>" + " </do>" + " <do type=\"accept\" label=\"End membership\" name=\"2\">" + " <go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">" + " <postfield name=\"action\" value=\"delete\"/>" + " </go>" + " </do>" + "</card>"); } } // View member information. // The "action" request parameter "action" has the value "view". else if (action.equals("view")) { out.println("<card title=\"View\" id=\"view\">" + " <p>Date of birth: " + dob + "<br/>" + "Sex: " + sex + "<br/>" + "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 {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -