OpenLayers学习笔记7——使用javaBean实现用户登录

之前的开发是采用phpStorm,前端:html+css+javascript+jquery,服务器端:php;但是这个前后台交互要通过ajax来实现数据交互,中间遇到了跨域的问题,卡了两天虽然解决了但是后面部署的话会比较麻烦。正好最近老板让我接手之前另一个项目开发的软件,是用jsp写的,一开始我是拒绝的,后来借了一本jsp的书,用一天的时间快速读了下,觉得这个开发部署要方便很多,于是就把之前的代码全部改成了jsp的。开发环境网上搜一搜都有的,我采用的是myEclipse+Tomcat;废话不多说,这里就记录下使用JavaBean实现用户登录。

UserBean代码:

/**
 * 
 */
package edu.whu.vge.util;

public class UserBean {

	private String username;
	private String password;
	private boolean logined = false;

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the logined
	 */
	public boolean isLogined() {
		return logined;
	}

	/**
	 * @param logined
	 *            the logined to set
	 */
	public void setLogined(boolean logined) {
		this.logined = logined;
	}

}

DBBean代码:

/**
 * 
 */
package edu.whu.vge.util;

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

public class DBBean {

	private Connection connection;
	private PreparedStatement pstat;
	private ResultSet reSet;

	public DBBean() {
		// TODO Auto-generated constructor stub
	}

	// 获取数据库连接
	public Connection getConnection() throws ClassNotFoundException {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/userinfo?user=root&password=root";
			connection = DriverManager.getConnection(url);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return connection;
	}

	// 执行数据库查询并返回表
	public ResultSet query(String sql) throws SQLException,
			ClassNotFoundException {

		connection = getConnection();
		pstat = connection.prepareStatement(sql);
		reSet = pstat.executeQuery();
		return reSet;

	}

	// 关闭数据库连接
	public void close() throws SQLException {

		if (reSet != null) {
			reSet.close();

		}
		if (pstat != null) {
			pstat.close();
		}
		if (connection != null) {
			connection.close();

		}
	}

}

chkUser.jsp

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=gb2312" pageEncoding="gb2312"%>

<%@ page import="java.sql.*"%>
<%@ page import="edu.whu.vge.util.DBBean"%>
<%@ page import="edu.whu.vge.util.UserBean"%>
<%--使用javaBean连接数据库实现用户登录 --%>
<jsp:useBean id="user" scope="session" class="edu.whu.vge.util.UserBean"></jsp:useBean>
<jsp:useBean id="mysqlDB" class="edu.whu.vge.util.DBBean"></jsp:useBean>
<jsp:setProperty property="*" name="user" />

<%
	String username = new String(user.getUsername().getBytes(
			"ISO-8859-1"), "GB2312");
	String password = user.getPassword();
	String sql = "select * from users where name='" + username
			+ "' and password='" + password + "'";

	ResultSet resultSet = mysqlDB.query(sql);
	if (resultSet.next()) {
	%>
	<script type="text/javascript">
	window.location.href = "index.jsp";
	</script>
	<%
	} else {
	%>
	<
	<script type="text/javascript">
	alert("用户名或密码错误,请核实!!!");
	window.location.href = "login.jsp";
	</script>
	<%
	}
	%>

这里的登录验证是与mysql中存储的用户信息,验证成功的话则采用javascript跳转的主界面,否则提示停留在登录界面。jsp的跳转方式参见博客:JSP页面跳转的几种实现方法   。这里还有一个需要注意的问题就是,当用户名为中文时,会出现乱码导致验证不成功,需要进行转码:

String username = new String(user.getUsername().getBytes(
			"ISO-8859-1"), "GB2312");

login.jsp代码:

<body>
	<div class="login_bj">
		<div class="login_mian">
			<form id="form1" name="form1" action="chkUser.jsp" method="post">
				<table cellpadding="0" cellspacing="0" border="0"
					class="login_table">
					<tr>
						<th>用户名</th>
						<td><input type="text" class="login_int" id="username"
							name="username" value="" /></td>
					</tr>
					<tr>
						<th>密码</th>
						<td><input type="password" class="login_int" id="password"
							name="password" value="" /></td>
					</tr>
					<tr>
						<th></th>
						<td><input type="button" class="btn_login"
							onclick="doSubmit();" /></td>
					</tr>
				</table>
			</form>
			<div class="footer" align="left">
				<span>Copyright @ 2015 武汉大学</span>
			</div>
		</div>
	</div>
	<div id="dialog-username">
		<p class="alert-message">请输入用户名!</p>
	</div>
	<div id="dialog-password">
		<p class="alert-message">请输入密码!</p>
	</div>
</body>

mysql数据库:

登录界面:

成功登录后界面:

初学jsp很多方面都还不懂,以练带学是很有效的方法,实现了简单的用户登录之后,下一步就是从数据库查询poi进行标注显示了,先设计一个好的用户交互方式,在使用JavaBean封装业务逻辑

转载自:https://blog.csdn.net/giser_whu/article/details/46549815

You may also like...