⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adminprotocolhandler.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2002 Nokia Corporation. // // THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER, // EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS // FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE // OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE // ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO // OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR // SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE // RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT // OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED // BY THIRD PARTIES // // Furthermore, information provided in this source code is preliminary, // and may be changed substantially prior to final release. Nokia Corporation // retains the right to make changes to this source code at // any time, without notice. This source code is provided for informational // purposes only. // // Nokia and Nokia Connecting People are registered trademarks of Nokia// Corporation.// Java and all Java-based marks are trademarks or registered trademarks of// Sun Microsystems, Inc.// Other product and company names mentioned herein may be trademarks or// trade names of their respective owners.// // A non-exclusive, non-transferable, worldwide, limited license is hereby // granted to the Licensee to download, print, reproduce and modify the // source code. The licensee has the right to market, sell, distribute and // make available the source code in original or modified form only when // incorporated into the programs developed by the Licensee. No other // license, express or implied, by estoppel or otherwise, to any other // intellectual property rights is granted herein.package example.fruitmachine;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;class AdminProtocolHandler {    private final UserDatabase userDatabase;    AdminProtocolHandler(UserDatabase userDatabase)    {        this.userDatabase = userDatabase;    }    void doGet(HttpServletRequest  request, HttpServletResponse response)        throws IOException, ServletException    {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        String pathInfo = request.getPathInfo();        String pathRoot = request.getContextPath() + request.getServletPath();        if (pathInfo == null)        {            response.sendError(HttpServletResponse.SC_BAD_REQUEST,                               "Missing path info");        }        else if (pathInfo.equals("/admin-create"))        {            writeCreateAccountPage(out, pathRoot);        }        else if (pathInfo.equals("/admin-delete"))        {            writeDeleteAccountPage(out, pathRoot);        }        else if (pathInfo.equals("/admin"))        {            writeListAccountPage(out, pathRoot);        }        else        {            response.sendError(HttpServletResponse.SC_BAD_REQUEST,                               "Unexpected path info");        }        out.close();    }    private void writeCreateAccountPage(PrintWriter out,                                        String pathRoot)        throws IOException    {        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Create Account</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Fruit Machine: Create Account</h1>");        out.println("<form method=\"POST\" action=\"" +                    pathRoot +                    "/admin-create\">");        out.println("<table>");        out.println("<tr><td>Name:</td>");        out.println("<td><input type=text size=16 maxlength=16 " +                    "name=name></td></tr>");        out.println("<tr><td>Password:</td>");        out.println("<td><input type=password size=8 maxlength=8 " +                    "name=password></td></tr>");        out.println("<tr><td colspan=2 align=center>");        out.println("<input type=submit value=\"Create\">");        out.println("</td></tr>");        out.println("</table>");        out.println("</form>");        out.println("<p>Note: accounts are not stored persistently in" +                    " this prototype.</p>");        out.println("</body>");        out.println("</html>");    }    private void writeDeleteAccountPage(PrintWriter out,                                        String pathRoot)        throws IOException    {        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Delete Account</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Fruit Machine: Delete Account</h1>");        out.println("<form method=\"POST\" action=\"" +                    pathRoot +                    "/admin-delete\">");        out.println("Delete an existing account:");        out.println("<table>");        out.println("<tr><td>Name:</td>");        out.println("<td><input type=text size=16 maxlength=16 " +                    "name=name></td></tr>");        out.println("<tr><td>Password:</td>");        out.println("<td><input type=password size=8 maxlength=8 " +                    "name=password></td></tr>");        out.println("<tr><td colspan=2 align=center>");        out.println("<input type=submit value=\"Delete\">");        out.println("</td></tr>");        out.println("</table>");        out.println("</form>");        out.println("<p>Note: accounts are not stored persistently in" +                    " this prototype.</p>");        out.println("</body>");        out.println("</html>");    }    private void writeListAccountPage(PrintWriter out,                                      String pathRoot)        throws IOException    {        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Accounts</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Fruit Machine: Accounts</h1>");        out.println("Existing accounts:");        out.println("<table>");        out.println("<tr><th>Name</th><th>Credit</th></tr>");        Iterator iter = userDatabase.getAllUsers().iterator();        while (iter.hasNext())        {            User user = (User)(iter.next());            String name = user.getName();            int credit = user.getCredit();            out.println("<tr><td>" + name + "</td>" +                            "<td>" + credit + "</td></tr>");        }        out.println("</table>");        out.println("<form method=\"GET\" action=\"" +                    pathRoot +                    "/admin-create\">");        out.println("<input type=submit value=\"Create\">");        out.println("</form>");        out.println("<form method=\"GET\" action=\"" +                    pathRoot +                    "/admin-delete\">");        out.println("<input type=submit value=\"Delete\">");        out.println("</form>");        out.println("<p>Note: accounts are not stored persistently in" +                    " this prototype.</p>");        out.println("</body>");        out.println("</html>");    }    void doPost(HttpServletRequest request, HttpServletResponse response)        throws IOException, ServletException    {        String pathInfo = request.getPathInfo();        if (pathInfo == null)        {            response.sendError(HttpServletResponse.SC_BAD_REQUEST,                               "Missing path info");        }        else if (pathInfo.equals("/admin-create"))        {            handleCreateAccountRequest(request, response);        }        else if (pathInfo.equals("/admin-delete"))        {            handleDeleteAccountRequest(request, response);        }        else        {            response.sendError(HttpServletResponse.SC_BAD_REQUEST,                               "Unexpected path info");        }    }            private void handleCreateAccountRequest(HttpServletRequest request,                                            HttpServletResponse response)        throws IOException    {        String name = request.getParameter("name");        String pathRoot = request.getContextPath() + request.getServletPath();        if ((name == null) || (name.length() == 0))        {            writeErrorPage(response, "Specify a name", pathRoot);        }        else if (userDatabase.getUser(name) != null)        {            writeErrorPage(response,                           "User \"" + name + "\" already exists",                           pathRoot);        }        else        {            String password = request.getParameter("password");            if (password == null)            {                password = "";            }            User user = userDatabase.createUser(name, password);            writeAccountCreatedPage(response, name, pathRoot);        }    }    private void handleDeleteAccountRequest(HttpServletRequest request,                                            HttpServletResponse response)        throws IOException    {        String name = request.getParameter("name");        String pathRoot = request.getContextPath() + request.getServletPath();        if ((name == null) || (name.length() == 0))        {            writeErrorPage(response, "Specify a name", pathRoot);        }        else        {            String password = request.getParameter("password");            if (password == null)            {                password = "";            }            User user = userDatabase.getUser(name);            if ((user == null) || !user.isCorrectPassword(password))            {                writeErrorPage(response,                               "Invalid name or password",                               pathRoot);            }            else            {                userDatabase.deleteUser(user);                writeAccountDeletedPage(response, name, pathRoot);            }        }    }    private void writeErrorPage(HttpServletResponse response,                                String message,                                String pathRoot)        throws IOException    {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Error</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Error</h1>");        out.println("<p>" + message + "</p>");        out.println("<a href=\"" + pathRoot +                    "/admin\">Back to account list</a>");        out.println("</body>");        out.println("</html>");        out.close();    }    private void writeAccountCreatedPage(HttpServletResponse response,                                         String name,                                         String pathRoot)        throws IOException    {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Account Created</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Account Created</h1>");        out.println("<p>Account " + name + " is now ready for use.</p>");        out.println("<a href=\"" + pathRoot +                    "/admin\">Back to account list</a>");        out.println("</body>");        out.println("</html>");        out.close();    }    private void writeAccountDeletedPage(HttpServletResponse response,                                         String name,                                         String pathRoot)        throws IOException    {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out.println("<html>");        out.println("<head>");        out.println("<title>Fruit Machine: Account Deleted</title>");        out.println("</head>");        out.println("<body>");        out.println("<h1>Account Deleted</h1>");        out.println("<p>Account " + name + " is now deleted.</p>");        out.println("<a href=\"" + pathRoot +                    "/admin\">Back to account list</a>");        out.println("</body>");        out.println("</html>");        out.close();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -