Jetty (b) Login implementation

Source: Internet
Author: User
Tags addchild extend stub

Interllij+jetty+cocos2d-js

Service side:

Jettyserver.java

Package com.mind.server;

Import Org.eclipse.jetty.server.Server;
Import Org.eclipse.jetty.servlet.ServletContextHandler;
Import Org.eclipse.jetty.servlet.ServletHolder;

public class Jettyserver {public

    static void Main (string[] args) throws Exception {
        Server server = new Server (80 );

        Servletcontexthandler context = new Servletcontexthandler (servletcontexthandler.sessions);
        Context.setcontextpath ("/");
        Server.sethandler (context);

        Http://localhost:8030/mysql
        Context.addservlet (New Servletholder (New Servletdatabase ()), "/mysql");

        try {
            server.start ();
            Server.join ();
        } finally {
            Server.destroy ();}}
}

Servletdatabase.java

Package com.mind.server;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import java.sql.Statement;
Import Javax.servlet.Servlet;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; 

    public class Servletdatabase extends HttpServlet implements Servlet {private static final long serialversionuid = 1L;
        /** * @see httpservlet#httpservlet () */public servletdatabase () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, HTTPSERVL Etresponse response) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servle Texception, IOException {}/** * @see httpservlet#dopost (HttpServletRequest reQuest, HttpServletResponse Response) */protected void DoPost (HttpServletRequest request, HttpServletResponse Res Ponse) throws Servletexception, IOException {//TODO auto-generated method stub String username = Request
        . GetParameter ("name");

        String Password = request.getparameter ("password");
        SYSTEM.OUT.PRINTLN (username);


        SYSTEM.OUT.PRINTLN (password);
        String Driver = "Com.mysql.jdbc.Driver";
        The URL points to the database name to access String URL = "Jdbc:mysql://192.168.19.130/test";
        MySQL configuration username String user = "root";

        Java connection MySQL configuration password String userpwd = "root";

            try {/** * Load driver */class.forname (driver);
            Continuous database Connection conn = drivermanager.getconnection (URL, user, userpwd);

            if (!conn.isclosed ()) System.out.println ("succeeded connecting to the database!"); statement used to execute SQLStatement Statement Statement = Conn.createstatement ();
            The SQL statement ID and content to execute are the items in table review.

            String sql = "SELECT * FROM mytable where name= '" +username+ "' and password= '" + Password + "'";

            Get results ResultSet rs = statement.executequery (SQL);
            PrintWriter toclient = Response.getwriter ();
                if (Rs.next ()) {System.out.println ("Logon");

            Toclient.println ("1");
            }else{System.out.println ("Login faild");

            }/** * Cross-domain settings */response.setheader ("Access-control-allow-origin", "*");
            Rs.close ();
        Conn.close ();
            } catch (ClassNotFoundException e) {System.out.println ("Sorry,can ' t find the driver!");
        E.printstacktrace ();
        } catch (SQLException e) {e.printstacktrace ();
        } catch (Exception e) {e.printstacktrace ();
 }   }
} 
Client

Cf. Loginlayer = cc.
    Layer.extend ({_usernametextfield:null, _passwordtextfield:null, _registerbutton:null, _loginButton:null,
        _activitycontroller:null, Ctor:function () {this._super ();
    return true;
        }, Onenter:function () {this._super (); var L = new CC.
        Labelttf ("Get infos via XHR", "Thonburi", 16);
        This.addchild (L, 1);
        l.x = CC.WINSIZE.WIDTH/2;

        L.y = cc.winsize.height-60; This._usernametextfield = new Ccui.
        TextField (); This._usernametextfield.setposition (CC.P (CC.WINSIZE.WIDTH/2, CC.WINSIZE.HEIGHT/2 +)) This._usernameTextField.s
        Etplaceholder ("Please enter your nickname");

        This.addchild (This._usernametextfield, 1); This._passwordtextfield = new Ccui.
        TextField (); This._passwordtextfield.setposition (CC.P (CC.WINSIZE.WIDTH/2, CC.WINSIZE.HEIGHT/2)) this._
        Passwordtextfield.setplaceholder ("Please enter your password");
    This._passwordtextfield.setpasswordenabled (TRUE);    This.addchild (This._passwordtextfield, 1); This._registerbutton = new Ccui.
        Button ();
        This._registerbutton.settouchenabled (TRUE);
        This._registerbutton.loadtextures (Res.register_button_png, "", "");
        This._registerbutton.addtoucheventlistener (this.ontouchevent, this); This._registerbutton.setposition (CC.P (CC.WINSIZE.WIDTH/2-This._registerbutton.getcontentsize (). WIDTH/2, This._
        Passwordtextfield.getposition (). Y-this._passwordtextfield.getcontentsize (). height-50));

        This.addchild (This._registerbutton, 1, 1); This._loginbutton = new Ccui.
        Button ();
        This._loginbutton.settouchenabled (TRUE);
        This._loginbutton.loadtextures (Res.login_button_png, "", "");
        This._loginbutton.addtoucheventlistener (this.ontouchevent, this); This._loginbutton.setposition (CC.P (CC.WINSIZE.WIDTH/2 + this._loginbutton.getcontentsize (). WIDTH/2, This._ Passwordtextfield.getposition (). Y-this._passwordtextfield.getcontentsize (). hEIGHT-50));
    This.addchild (This._loginbutton, 1, 2); }, Sendpostforms:function () {var statuspostlabel = new CC.
        Labelttf ("Status:", "Thonburi", 12);
        This.addchild (Statuspostlabel, 1);
        statuspostlabel.x = CC.WINSIZE.WIDTH/10 * 7;
        Statuspostlabel.y = cc.winsize.height-100;
        This.ensureleftaligned (Statuspostlabel);

        Statuspostlabel.setstring ("Status:send Post Request to httpbin.org width form data"); var responselabel = new CC.
        Labelttf ("", "Thonburi", 16);
        This.addchild (Responselabel, 1);
        This.ensureleftaligned (Responselabel);
        responselabel.x = CC.WINSIZE.WIDTH/10 * 7;

        Responselabel.y = CC.WINSIZE.HEIGHT/2;
        var xhr = Cc.loader.getXMLHttpRequest ();
        This.streamxhreventstolabel (XHR, Responselabel, Responselabel, "POST");

        Xhr.open ("POST", "Http://192.168.199.132:8030/mysql"); Set Content-type "application/x-www-form-urlencoded" to post form data
        Mulipart/form-data for upload xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
        /** form: {"name": "Liuwei", "Password": "123456"} **/
        var args = "Name=" + this._usernametextfield.getstring () + "&password=" + this._passwordtextfield.getstring ();
    Xhr.send (args); }, Scrollviewdidscroll:function (view) {}, Scrollviewdidzoom:function (view) {}, Ontouchevent:functi On (sender, type) {switch (type) {case Ccui.
            Widget.TOUCH_BEGAN:break; Case Ccui.
            Widget.TOUCH_MOVED:break; Case Ccui.  Widget.TOUCH_ENDED:if (Sender.gettag () = = 2) {if (this._usernametextfield.getstring ()
                    && this._passwordtextfield.getstring ()) {this.sendpostforms ();
 }} else if (sender.gettag () = = 1) {                   }}, Streamxhreventstolabel:function (XHR, Label, TextBox, method {//Simple events [' Loadstart ', ' abort ', ' Error ', ' Load ', ' loadend ', ' timeout '].foreach (function (even
            Tname) {xhr["on" + eventname] = function () {label.string + = "\nevent:" + eventname
        }
        });
        Special event var = this; Xhr.onreadystatechange = function () {if (xhr.readystate = = 4 && (xhr.status >= && xhr
                . Status <= 207) {var httpstatus = Xhr.statustext;
                Cc.log ("httpstatus==>" + httpstatus);
                    if (xhr.responsetext) {that._usernametextfield.setstring ("");
                    That._passwordtextfield.setstring ("");
                    That._loginbutton.settouchenabled (FALSE);
                    That._registerbutton.settouchenabled (FALSE); CC.director.runscene (new Gamemainscene);
                Cc.log ("Login in");
                    }else{that._usernametextfield.setstring ("");
                    That._passwordtextfield.setstring ("");
                Cc.log ("Login Out");
            } cc.log ("xhr.responsetext==>" + xhr.responsetext);
        }}}, ensureleftaligned:function (label) {Label.anchorx = 0;
        Label.anchory = 1; Label.textalign = cc.
    Text_alignment_left;

}
}); Cf. Loginscene = cc.
        Scene.extend ({onenter:function () {this._super (); var layer = new cf.
        Loginlayer ();
    This.addchild (layer); }
});

Operation Result:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.