Android_登录之服务器逻辑

    《Android_登录之客户端逻辑》文中讲到LoginActivity获取账户和密码框字符,通过servlet(LoginServlet)传递数据至服务器,并接受反馈信息以处理登录逻辑。下面来详解LoginServlet的实现方法。

1.LoginServlet

package com.amaker.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.amaker.dao.UserDao;
import com.amaker.dao.impl.UserDaoImpl;
import com.amaker.entity.User;
/*
 * 实现登陆功能
 * @author shichengwu
 * @site www.shichengwu.com
 */
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		UserDao dao = new UserDaoImpl();
		// 获得客户端请求参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("获得用户名"+username);
		System.out.println("获得密码"+password);
		User u = dao.login(username, password);
		if(u!=null){
			// 响应客户端内容,登录成功
			out.print(build(u));
		}else{
			// 响应客户端内容,登录失败
			out.print("0");
		}
		out.flush();
		out.close();
	}
	
	private String build(User u){
		String userMsg = "";
		userMsg+="id="+u.getId();
		userMsg+=";";
		userMsg+="name="+u.getTruename();
		return userMsg;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	public void init() throws ServletException {
	}	
	public LoginServlet() {
		super();
	}
	public void destroy() {
		super.destroy();
	}
}

 

2.数据库封装实体类 User

package com.amaker.entity;

/**
 * 用于封装User表的实体类
 * @author shichengwu
 * @site www.shichengwu.com
 */
public class User {
	// 编号
	private int id;
	// 账号
	private String username;
	// 密码
	private String password;
	// 用户名称
	private String truename;
	// 性别

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getTruename() {
		return truename;
	}
	public void setTruename(String truename) {
		this.truename = truename;
	}	
}

 

3.接口UserDao

package com.amaker.dao;

import com.amaker.entity.User;
/* UesrDao 接口
 * @author shichengwu
 * @site www.shichengwu.com
 */
public interface UserDao {
	// 登录方法
	public User login(String username,String password);
	public User login1(String username, String password);
}

 4.数据库交互实现 UserDaoImpl

package com.amaker.dao.impl;

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

import com.amaker.dao.UserDao;
import com.amaker.entity.User;
import com.amaker.util.DBUtil;
/**
 * 用户登录DAO实现类
 * @author shichengwu
 * @site www.shichengwu.com
 */
public class UserDaoImpl implements UserDao {
	
	/**
	 * 通过用户名称和密码登录,登录成功返回User对象,登录失败返回null
	 */
	public User login(String username, String password) {
		// 查询SQL语句
		String sql = " select id,username,password,true_name from kl_user"+
						" where username=? and password=?";
		// 数据库连接工具类
		DBUtil util = new DBUtil();
		// 获得连接
		Connection conn = util.openConnection();
		try {
			// 获得预定义语句
			PreparedStatement pstmt = conn.prepareStatement(sql);
			// 设置查询参数
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			// 执行查询
			ResultSet rs = pstmt.executeQuery();
			// 判断用户是否存在
			if (rs.next()) {
				// 获得用户信息
				int id = rs.getInt(1);
				String truename = rs.getString(4);
				// 封装用户信息
				User u = new User();
				
				u.setId(id);
				u.setUsername(username);
				u.setPassword(password);
				u.setTruename(truename);
				return u;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			util.closeConn(conn);
		}
		return null;
	}


5 JDBC配置
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.1.49:1521:orcl
username=XX
password=XX

至此,服务器端设计完毕,加上上一篇文章设计的客户端逻辑,一个完整的登陆实例构建成功,至于页面Layout,画一个账户输入框、一个密码输入框,外加一个button点击登录,就很容易了,登录逻辑、账户及密码框的获取验证在本文和上文中均已完成。

你可能感兴趣的:(android,数据库,servlet,jdbc,服务器,登录,login)