<javaweb> 向外提供json数据接口(登录注册)

Servlet提供登录注册接口

总想学学后台的东西,自己用Servlet写了一个很low的注册登录的功能,上我那low到家的代码,首先弄了数据库表环境等等一些外在条件,先来数据库类吧:

public class DbHelper {
public LoginBean loginBean ;
/**
* 注册
* @param map
* @return
*/
public int register(Map<String, String> map){
String name = map.get("name");
String registerID = map.get("registerID");
String wxOpenID = map.get("wxOpenID");
String nickName = map.get("nickName");
String password = map.get("password");
String sex = map.get("sex");
String age = map.get("age");
String address = map.get("address");

try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ooshop", "root",
"123456");
Statement stmt = conn.createStatement();

int a=1;
String sql = "insert into user (userID,registerID,wxOpenID,name,nickName,password,sex,age,address) values" +
" (null,'"+registerID+"','"+wxOpenID+"','"+name+"','"+nickName+"','"+password+"','"+sex+"','"+age+"','"+address+"')";

int executeUpdate = stmt.executeUpdate(sql);
System.out.print("插入结果:"+executeUpdate);

return executeUpdate;

}catch(Exception e){
System.out.print("异常结果:"+e);
}
return 0;

}



/**
 * 登录
 * @param map
 */
public int loginQuery(Map<String, String> map) {
// TODO Auto-generated method stub

String name = map.get("name");
String password = map.get("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ooshop", "root",
"123456");
Statement stmt = conn.createStatement();


String sql = "select * from user where name='"+name+"'";
ResultSet rs = stmt.executeQuery(sql);//创建数据对象
 
if(rs.next()){
String password_sql = rs.getString(6);
if(password_sql.equals(password)){
Map<String, String> result = new HashMap<String, String>();
String name_lg = rs.getString(4);
String nickName_lg = rs.getString(5);
String sex_lg = rs.getString(7);
String age_lg = rs.getString(8);
String address_lg = rs.getString(9);
result.put("name", name_lg);
result.put("nickName", nickName_lg);
result.put("sex", sex_lg);
result.put("age", age_lg);
result.put("address", address_lg);
loginBean = new LoginBean();
loginBean.setResult(result);
 
return 1;
}else{
return 2;
}
             }else{
            return -1;
             }



}catch(Exception e){
System.out.print("异常结果:"+e);
return 500;
}


}


然后是注册类:

public class register extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Map<String,String> map=new HashMap<String,String>();
String registerID = new String(request.getParameter("registerID").getBytes("ISO-8859-1"), "UTF-8");
String wxOpenID = new String(request.getParameter("wxOpenID").getBytes("ISO-8859-1"), "UTF-8");
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"), "UTF-8");
String nickName = new String(request.getParameter("nickName").getBytes("ISO-8859-1"), "UTF-8");
String sex = new String(request.getParameter("sex").getBytes("ISO-8859-1"), "UTF-8");
String age = new String(request.getParameter("age").getBytes("ISO-8859-1"), "UTF-8");
String address = new String(request.getParameter("address").getBytes("ISO-8859-1"), "UTF-8");

map.put("name", name);
map.put("registerID", registerID);
map.put("wxOpenID", wxOpenID);
map.put("nickName", nickName);
map.put("password", password);
map.put("sex", sex);
map.put("age", age);
map.put("address", address);

DbHelper dbHelper = new DbHelper();
int result = dbHelper.register(map);

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8"); // 解决中文乱码问题
ArrayList<Map> list=new ArrayList<Map>();
Map<String, Comparable> map1 = new HashMap<String, Comparable>();
try {
if(result==1){
//returnData("",response);

PrintWriter out = response.getWriter();
map1.put("code", "200");
map1.put("msg", "提交成功");
map1.put("data", null);
JSONObject json = JSONObject.fromObject(map1);
out.write(json.toString());

}else{

PrintWriter out = response.getWriter();
map1.put("code", "1");
map1.put("msg", "提交失败");
map1.put("data", null);
JSONObject json = JSONObject.fromObject(map1);
out.write(json.toString());
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}
}


然后登陆类:

public class login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String name = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"), "UTF-8");

Map<String,String> map=new HashMap<String,String>();
map.put("name", name);
map.put("password", password);

response.setContentType("text/html");
response.setCharacterEncoding("UTF-8"); // 解决中文乱码问题
ArrayList<Map> list=new ArrayList<Map>();
Map<String, Comparable> map1 = new HashMap<String, Comparable>();
PrintWriter out = response.getWriter();

DbHelper dbHelper = new DbHelper();
int result =dbHelper.loginQuery(map);
JSONObject json ;
switch(result){

case -1:

map1.put("code", "-1");
map1.put("msg", "用户不存在");
map1.put("data", null);
json = JSONObject.fromObject(map1);
out.write(json.toString());

break;

case 1: 
 
Map<String, String> result2 = dbHelper.loginBean.getResult();
JSONObject json1 = JSONObject.fromObject(result2);
map1.put("code", "200");
map1.put("msg", "登录成功");
map1.put("data", json1.toString());
json = JSONObject.fromObject(map1);
out.write(json.toString());

break;

case 2:
map1.put("code", "1");
map1.put("msg", "密码有误");
map1.put("data", null);
json = JSONObject.fromObject(map1);
out.write(json.toString());
break;


case 500:
map1.put("code", "500");
map1.put("msg", "查询异常,服务器错误。");
map1.put("data", null);
json = JSONObject.fromObject(map1);
out.write(json.toString());

break;

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

}


最后放一个domain

public class LoginBean {

private Map<String, String> result;
public Map<String, String> getResult() {
return result;
}

public void setResult(Map<String, String> result) {
this.result = result;
}

}



运行效果:

注册:

http://192.168.0.117:8080/xxshop/register?name=张伟&registerID=rs1&wxOpenID=rs2&nickName=张益达&password=000000&sex=女&age=18&address=北京



登录:

http://192.168.0.117:8080/xxshop/servlet/login?name=张伟&password=000000




安卓端就不贴代码了,太过于简单,至此,就算完成了,代码里面有很多不妥的地方,看看就好,别误人子弟!

猜你喜欢

转载自blog.csdn.net/csdn_lg_one/article/details/77784318
今日推荐