Android课程表服务器端实现方案

服务器端开发

1.开发环境

开发平台:Eclipse J2EE (LUNA版本)
开发语言:Java
服务器:Apache tomcat v7.0
数据库:Mysql
框架:Struts2

2.设计

服务器端主要负责将客户端所传上来的数据进行分析处理,并访问服务器端数据库进行CRUD操作,最后将处理结果传回客户端。

本服务器端的主要业务逻辑可以分为两层:控制层与数据处理层。控制层主要实现了与客户端交互的接口;处理层主要提供数据处理操作。

具体的提供一个登录代码模板以供学习:

1、action,将客户端传来的request进行处理:

package com.miao.action;

import java.io.DataOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.miao.daoimp.UserDaoImp;

public class LoginAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware {
    /**
* 
*/
    private static final long serialVersionUID = 1L;

    HttpServletRequest request;
    HttpServletResponse response;

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void login() {
        try {

            this.response.setContentType("text/html;charset=utf-8");
            this.response.setCharacterEncoding("UTF-8");

            DataOutputStream dos = new DataOutputStream(response.getOutputStream());

            String username = this.request.getParameter("username");
            String password = this.request.getParameter("password");

            UserDaoImp userDaoImp = new UserDaoImp();
            String b = userDaoImp.login(username, password);
            System.out.println("username = " + username + "password = " + password);
            System.out.println("loginResult = " + b);
            dos.writeUTF(b);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

2、daoimp,运用google的Gson进行封装后返回:

package com.miao.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import com.miao.db.GetConn;
import com.google.gson.Gson;

public class UserDaoImp {
    public String login(String username,String password) 
    {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        GetConn getConn=new GetConn();
        ResultSet rs = null;
        ResultSet rs1 = null;
        Connection conn=getConn.getConnection();
        try {
            PreparedStatement ps=conn.prepareStatement("select * from user_msg where username=?");
            ps.setString(1,username);
            rs=ps.executeQuery();
            if(rs.next())
            {
                String pwd = rs.getString("password");
                if(!password.equals(pwd)) {//密码不正确
                    resultMap.put("result_code", 1);
                } else {
                    //密码正确,直接做成json格式传回
                    resultMap.put("result_code", 0);
                    resultMap.put("uid", rs.getInt("uid"));
                    resultMap.put("username", rs.getString("username"));
                    resultMap.put("gender", rs.getString("gender"));                    
                    resultMap.put("phone", rs.getString("phone"));
                    resultMap.put("headshot", rs.getString("headshot"));
                }
            }
            else{
                resultMap.put("result_code", 2);//未注册
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            getConn.closeconn(conn);
        }
        return (new Gson()).toJson(resultMap);
    }
}

3、db连接池:

package com.miao.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class GetConn {
    public Connection getConnection() 
    { 
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/miao?useUnicode=true&characterEncoding=utf8","root","");
        } catch (ClassNotFoundException e) {            
            e.printStackTrace();
        } catch (SQLException e) {  
            e.printStackTrace();
        }       
        return connection;
    }
    public void closeconn(Connection connection)
    {    
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值