java、web Util

本文介绍了一种用于注册和登录的后端校验方法,包括字符串判断、注册及登录信息验证流程,并展示了如何使用Java连接MySQL和SQL Server数据库。

数据库、字符串判断、web Utils

1、判断字符串是否为空
2、注册后端校验模板
3、登录后端校验模板
4、mysql数据库连接
5、sqlserver数据库连接
6、html转jsp

字符串判断

public class StringUtils {
	public static boolean isEmpty(String util){
		if("".equals(util)||util==null)
			return true;
		return false;
	}
}

注册后端校验

public class Register extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String username = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");// 从jsp界面获取的中文需要用ISO-8859-1解码
		String pwd1 = new String(request.getParameter("pwd1").getBytes("ISO-8859-1"), "UTF-8");// 从jsp界面获取的中文需要用ISO-8859-1解码
		String pwd2 = new String(request.getParameter("pwd2").getBytes("ISO-8859-1"), "UTF-8");// 从jsp界面获取的中文需要用ISO-8859-1解码
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		if ("".equals(username) || username == null) {
			String a = URLEncoder.encode("用户名为空,请重新输入", "UTF-8"); 
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='regist.html';</script>");
			return;
			
		} else if("".equals(pwd1) || pwd1 == null){
			response.setCharacterEncoding("UTF-8");
			String a = URLEncoder.encode("密码不能为空,请重新输入", "UTF-8"); 
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='regist.html';</script>");
			return;
		}else if(!pwd1.equals(pwd2)){
			response.setCharacterEncoding("UTF-8");
			String a = URLEncoder.encode("两次密码不相同,请重新输入", "UTF-8"); 
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='regist.html';</script>");
			return;
		}else{
			AdminDao dao = new AdminDao();
			boolean regist = dao.regist(username, pwd1);
			if(regist){
				response.setCharacterEncoding("UTF-8");//设置页面编码格式
				String a = URLEncoder.encode("注册成功!请登录", "UTF-8"); 
				out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='login.html';</script>");
			}else{
				response.setCharacterEncoding("UTF-8");//设置页面编码格式
				String a = URLEncoder.encode("注册失败", "UTF-8"); 
				out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='regist.html';</script>");
			}
		}
	}
}

登录后端校验

public class Login extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = new String(request.getParameter("name").getBytes("ISO-8859-1"), "UTF-8");// 从jsp界面获取的中文需要用ISO-8859-1解码
		String password = new String(request.getParameter("pwd1").getBytes("ISO-8859-1"), "UTF-8");// 从jsp界面获取的中文需要用ISO-8859-1解码
		PrintWriter out = response.getWriter();
		if ("".equals(username) || username == null) {
			response.setCharacterEncoding("UTF-8");
			String a = URLEncoder.encode("用户名为空,请重新输入", "UTF-8"); 
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='login.html';</script>");
			return;
			
		} else if("".equals(password) || password == null){
			response.setCharacterEncoding("UTF-8");
			String a = URLEncoder.encode("密码不能为空,请重新输入", "UTF-8"); 
			out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='login.html';</script>");
			return;
		}else{
			AdminDao dao = new AdminDao();
			boolean login = dao.login(username, password);
			if(login){
				String a = URLEncoder.encode("登录成功,欢迎登录酒店管理系统!", "UTF-8"); 
				out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='HotelManage.html';</script>");
			}else{
				response.setCharacterEncoding("UTF-8");//设置页面编码格式
				String a = URLEncoder.encode("用户名或密码错误,请重新输入", "UTF-8"); 
				out.print("<script language='javascript'>alert(decodeURIComponent('"+a+"'));window.location.href='login.html';</script>");
			}
		}
	}
}

mysql数据库连接

public class Dbutil {
	private static String dbURL = "jdbc:mysql://localhost:3306/databaseName";
	private static String dbUser = "root";//用户名
	private static String dbPasswd = "123";//密码

	public Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection(dbURL, dbUser, dbPasswd);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public void close(Connection con) throws Exception {
		if (con != null)
			con.close();
	}
}

sql server数据库连接

public class conns {
	public Connection connection = null;
	public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static final String DBURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=name";
	//1433是默认端口号,一般不用改,name写数据库名字
	public static final String DBUSER = "sa";
	public static final String DBPASS = "123";

	public Connection getConnection() {
		try {
			Class.forName(conns.DBDRIVER);
		} catch (ClassNotFoundException e) {
			System.out.println("error connects sql.");
			e.printStackTrace();
		}
		try {
			connection = DriverManager.getConnection(conns.DBURL, conns.DBUSER, conns.DBPASS);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public static void close(Connection con) {
		if (con != null)
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
}

html页面转jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值