使用JSON技术结合MVC模式C标签实现登录功能

首先列出需要的工具

1、json使用的6个jar包

commons-beanutils.jar

commons-collections.jar

commons-lang-2.4.jar

commons-logging.jar

ezmorph-1.0.4.jar

json-lib-2.2.1-jdk15.jar

2、C标签的两个jar包

jstl.jar

standard.jar

3、mysql的链接包

mysql-connector-java-5.1.7-bin.jar

4、使用jquery的包

jquery-3.1.1.min.js

下面就二话不说上代码了

一)com.bihua.util数据库链接包

DBHelper类

package com.bihua.util;

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

public class DBHelper {
	private static String DRIVERCLASS="com.mysql.jdbc.Driver";//驱动
	private static String URL="jdbc:mysql://localhost:3306/test";//数据库路径
	private static String UNAME="root";//账号名
	private static String UPWD="1234";//密码
        //加载驱动
       static{
		try {
			Class.forName(DRIVERCLASS);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
      //建立链接
     public static Connection getConn(){
		Connection conn=null;
		try {
                       //获取链接
			conn=DriverManager.getConnection(URL,UNAME,UPWD);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
       //关闭链接
	public static void myClose(Connection conn,PreparedStatement ps,ResultSet rs){
		try {
			if(conn!=null&&conn.isClosed()){
				conn.close();
			}
			if(ps!=null){
				conn.close();
			}
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
二)com.zking.entity实体类

Login类

package com.bihua.entity;

public class Login {
	private String uname;//用户名
	private String upwd;//密码
        //无参构造
	public Login() {
	}
        //有参构造
	public Login(String uname, String upwd) {
		this.uname = uname;
		this.upwd = upwd;
	}
        //getset方法
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpwd() {
		return upwd;
	}
	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
	
}

三)com.zking.dao方法包

LoginDaoImp接口类

package com.bihua.dao;

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

import com.bihua.entity.Login;
import com.bihua.util.DBHelper;

public class LoginDaoImp implements LoginDao{
	/**
	 * 是否登陆成功的方法
	 * @param login
	 * @return
	 */
	public boolean goLogin(Login login) {
		boolean b=false;
		Connection conn=DBHelper.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from login where uname=? and upwd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, login.getUname());
			ps.setString(2, login.getUpwd());
			rs = ps.executeQuery();
			b=rs.next();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBHelper.myClose(conn, ps, rs);
		}
		return b;
	}

}
LoginDao实现类

package com.bihua.dao;

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

import com.bihua.entity.Login;
import com.bihua.util.DBHelper;

public class LoginDaoImp implements LoginDao{
	/**
	 * 是否登陆成功的方法
	 * @param login
	 * @return
	 */
	public boolean goLogin(Login login) {
		boolean b=false;
		Connection conn=DBHelper.getConn();
		PreparedStatement ps=null;
		ResultSet rs=null;
                //定义sql语句
		String sql="select * from login where uname=? and upwd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, login.getUname());
			ps.setString(2, login.getUpwd());
			rs = ps.executeQuery();
			b=rs.next();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
                       //关闭链接
			DBHelper.myClose(conn, ps, rs);
		}
		return b;
	}

}

四)com.zking.biz  biz包

LoginBiz接口类

package com.bihua.biz;

import com.bihua.entity.Login;

public interface LoginBiz {
	/**
	 * 是否登陆成功的方法
	 * @param login
	 * @return
	 */
	public boolean goLogin(Login login);
}
LoginBizImp实现类

package com.bihua.biz;

import com.bihua.dao.LoginDao;
import com.bihua.dao.LoginDaoImp;
import com.bihua.entity.Login;

public class LoginBizImp implements LoginBiz{
	private LoginDao loginDao=null;
	
	public LoginBizImp(){
		loginDao=new LoginDaoImp();
	}

	public boolean goLogin(Login login) {
		return loginDao.goLogin(login);
	}

}
五)com.zking.servlet  业务逻辑包

LoginServlet  登陆的业务逻辑类

package com.bihua.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.bihua.biz.LoginBiz;
import com.bihua.biz.LoginBizImp;
import com.bihua.entity.Login;

import net.sf.json.JSONObject;

public class LoginServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=UTF-8");
		//接收从页面上传过来的字符串
		String loginStr=req.getParameter("login");
		//把从页面上传过来的String类型的值转成JSON型字符串
		JSONObject jsonObject=JSONObject.fromObject(loginStr);
		//把JSON字符串转化为Login对象
		Login login=(Login) jsonObject.toBean(jsonObject,Login.class);
		//拿值判断
		LoginBiz loginBiz=new LoginBizImp();
		boolean b=loginBiz.goLogin(login);
		String str="";
		if(b){
			str="登陆成功";
		}else{
			str="登陆失败";
		}
		PrintWriter pw=resp.getWriter();
		pw.write(str);
		pw.close();
	}
	

}
六)登陆界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	
	
	
  
  
  
  	
登陆名:
登陆密码:
七)web.xml配置



  
    index.jsp
  
  
  
  	loginServlet
  	com.zking.servlet.LoginServlet
  
  
  
  	loginServlet
  	/login.do
  


登陆的效果就是这样子了,有什么问题可以私信我,欢迎大家指点一二


你可能感兴趣的:(学习,Java)