您的位置:首页 > 其它

Jetty(二)登录实现

2016-05-11 21:30 429 查看
InterlliJ+Jetty+Cocos2d-JS

服务端:

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(8030);

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, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) 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";
// URL指向要访问的数据库名
String url = "jdbc:mysql://192.168.19.130/test";
// MySQL配置时的用户名
String user = "root";
// Java连接MySQL配置时的密码
String userpwd = "root";

try {
/**
* 加载驱动程序
*/
Class.forName(driver);

// 连续数据库
Connection conn = DriverManager.getConnection(url, user, userpwd);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");

// statement用来执行SQL语句
Statement statement = conn.createStatement();

// 要执行的SQL语句id和content是表review中的项。
String sql = "select * from mytable where name='"+username+"' and password='" + password +"'";

// 得到结果
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");
}
/**
* 跨域设置
*/
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();
}
}
}
客户端

cf.LoginLayer = cc.Layer.extend({
<pre name="code" class="javascript">    _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 + 100))
this._usernameTextField.setPlaceHolder("请输入您的昵称");
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("请输入您的密码");
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:function (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 (eventname) {
xhr["on" + eventname] = function () {
label.string += "\nEvent : " + eventname
}
});
// Special event
var that = this;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && 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);
}
});



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: