服务端编写主要用到的工具是Sevlert语言,和myeclipse编程工具。
注册部分核心代码:
package rico_cliente;
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 java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA.Object;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/*
* http://localhost:8080/maiji/register.do?Data={"LoginName":"tom","Interesting":["game","sports"]}
*/
public class RegisterServlet4AndroidClient extends HttpServlet {
public RegisterServlet4AndroidClient() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
/*
* Data= {
* "LoginName":"tom"
* "Interesting":["game","sports"]
* }
*/
String data = request.getParameter("Data");
System.out.println(data);
/* json数据的解析|剥离*/
/*
* json-lib解析JSON数据的核心代码
*/
JSONObject object = JSONObject.fromObject(data);
String loginName = object.getString("LoginName");
System.out.println("注册的登录名是:"+loginName);
String loginPassword = object.getString("LoginPassword");
System.out.println("注册的登录密码是:"+loginPassword);
// JSONArray array = object.getJSONArray("Interesting");
// System.out.println("注册的兴趣爱好有:");
// if(array != null){
// for (Object obj : array) {
// System.out.print(obj.toString() +"\t");
// }
// }
// for (int i = 0; i < array.length(); i++) {
//
// }
/*
* 业务处理
*/
request.setCharacterEncoding("gb2312");
response.setContentType("text/heml;charset=gb2312");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
ResultSet rs = null;
Statement stmt = null;
Connection cnn =null;
/*
* 响应:封装json结果
* {
* "result":"success"
* "errorMsg":""
*
* }
* {
* "result":"failed"
* "errorMsg":"服务器处理错误,注册失败"
*
* }
*/
/**
* 返回值
* */
PrintWriter out=null;
try{
String user = "root";
String password = "123456";
out=response.getWriter();
ResultJSONBean jsonBean = new ResultJSONBean();
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载数据库驱动程序");
cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidclent", user, password);
stmt = cnn.createStatement();
rs = stmt.executeQuery("SELECT * FROM cloentregister where emp_name = '"+loginName+"'");
if (rs.next()) {
jsonBean.setResult("failed");
jsonBean.setErrorMsg("登陆名已被使用");
JSONObject obj = JSONObject.fromObject(jsonBean);
System.out.println(obj.toString());
out.print(obj.toString());
}else{
stmt.executeUpdate("INSERT INTO cloentregister(emp_name,emp_passward)VALUES('"+loginName+"','"+loginPassword+"')");
jsonBean.setResult("success");
jsonBean.setErrorMsg("");
JSONObject obj = JSONObject.fromObject(jsonBean);
System.out.println(obj.toString());
out.print(obj.toString());
}
/**************************************************/
// rs = stmt.executeQuery("SELECT * FROM cloentregister");
// while (rs.next()) {
// System.out.println(rs.getString("emp_name"));
//
// }
// jsonbean.setResult("success");
// jsonbean.setErrorMsg("");
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
if(out!=null){
out.close();
}
try {
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(cnn != null){
cnn.close();
cnn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
登录部分核心代码:
package rico_cliente;
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.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* http://localhost:8080/maiji/begin.du
*http://localhost:8080/maiji/begin.du?LoginName=tom&LoginPassword=123
* */
public class Rico_sndddb extends HttpServlet {
public Rico_sndddb() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("get");
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("post");
request.setCharacterEncoding("UTF-8");
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
/*
* 取值
* */
String loginName=request.getParameter("LoginName");
String loginPassword=request.getParameter("LoginPassword");
System.out.println(loginName);
System.out.println(loginPassword);
//避免乱码
response.setCharacterEncoding("UTF-8");
/*
* 默认的
* text/html
* */
response.setContentType("text/html;charset=UTF-8");
/**
* 返回值
* */
request.setCharacterEncoding("gb2312");
response.setContentType("text/heml;charset=gb2312");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
ResultSet rs = null;
Statement stmt = null;
Connection cnn =null;
PrintWriter out=null;
try{
out=response.getWriter();
/**
* 登陆的业务判断*/
boolean bool = false;
String name;
String spassword;
String user = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载数据库驱动程序");
cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidclent", user, password);
stmt = cnn.createStatement();
rs = stmt.executeQuery("SELECT * FROM cloentregister");
while (rs.next()) {
name=rs.getString("emp_name");
spassword=rs.getString("emp_passward");
if(loginName.equals(name)&&loginPassword.equals(spassword)){
//登陆正确
bool=true;
break;
}
}
if(bool){
//登陆正确
out.print("success");
}
else{
//登陆失败
out.print("failed");
}
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
if(out!=null){
out.close();
}
}
}
}