使用jsp技术完成界面的登录及注册
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.homework4.utils.DbConnUtils;
public class DoLogin {
//验证登录是否成功
public String doLogin(String _username,String _password){
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;
String result="";
String sql="select * from loginuser where userName=? and userPwd=?";
try {
conn=DbConnUtils.getConnection();
pst=conn.prepareStatement(sql);
pst.setString(1, _username);
pst.setString(2, _password);
rs= pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs.next()){
result= "success";
}else{
result= "fail";
}
DbConnUtils.closeAll(rs, pst, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
public class Employee {
private String RegisterName;
private String repassword;
private int sex;
private String prical;
private String email;
public String getRegisterName() {
return RegisterName;
}
public void setRegisterName(String registerName) {
RegisterName = registerName;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getPrical() {
return prical;
}
public void setPrical(String prical) {
this.prical = prical;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.homework4.utils.DbConnUtils;
import com.homework2.pojo.Employee;
public class DoRegister {
public String doRegister(Employee _employee){
Connection conn=null;
PreparedStatement pst=null;
String result="";
int count=0;
String sql="insert into loginuser(userName,userPwd,sex,princal,email) values(?,?,?,?,?,?)";
try {
conn=DbConnUtils.getConnection();
pst=conn.prepareStatement(sql);
pst.setString(1, _employee.getRegisterName());
pst.setString(2, _employee.getRepassword());
pst.setInt(3, _employee.getSex());
pst.setString(4, _employee.getPrical());
pst.setString(5, _employee.getEmail());
count=pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
if(count>0){
result= "success";
}else{
result= "fail";
}
DbConnUtils.closeAll(null,pst, conn);
return result;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DbConnUtils {
static String url="jdbc:mysql://127.0.0.1:3306/hospital1?useUnicode=true&characterEncoding=UTF-8";
static String username="root";
static String password="root";
public static Connection getConnection(){
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
//创建数据库连接
conn=DriverManager.getConnection(url, username, password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,Statement st,Connection conn){
//关闭结果集
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static ResultSet getQueryList(Connection _conn,Statement _st,String _sql){
ResultSet rs=null;
try {
rs=_st.executeQuery(_sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/common.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
</head>
<body>
<div id="regiter">
</div>
<div>
<form action="doLogin.jsp" method="post">
<table>
<tr>
<td><span style="font-size: large; font-weight: 600;"> 登录系统</span></td>
</tr>
<tr>
<td><input type="text" name="username" value="账号" size="30" maxlength="15" style="border-color: cornflowerblue;" id="d1" onblur="checkUsername()" /><div id="d1Div"></div></td>
</tr>
<tr>
<td><input type="password" name="pwd" size="30" maxlength="20" style="border-color: cornflowerblue;" id="d2"onblur="checkpassword()" /><div id="d2Div"></div></td>
</tr>
<tr>
<td><span style="color:red;">
<%-- <%
if (session.getAttribute("errors") != null) {
out.print(session.getAttribute("errors"));
}
%> --%>
<%
if (session.getAttribute("errors") != null) {
%>
<%=session.getAttribute("errors")%>
<%
}
%>
</span>
</td>
</tr>
<tr>
<td><input type="text" name="code" value="验证码" size="15" maxlength="4" style="border-color: cornflowerblue;" id="d3" onblur="checkcode()" /><span style="font-size: x-small;">7236//</span><div id="d3Div"></div></td>
</tr>
<tr>
<td><input type="submit" value="登录" />
<a href="regiter.jsp">注册</a>
<input type="reset" value="取消" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*,com.openlab.login.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doLogin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//处理post请求乱码
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
DoLogin doLogin = new DoLogin();
String result = doLogin.doLogin(username, password);
if (result.equals("success")) {
//登陆成功
request.setAttribute("login", username);
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
} else {
session.setAttribute("errors", "用户名密码不正确,请重新输入...");
//登陆失败
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'regiter.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/common.css">
</head>
<body>
<h2>欢迎来西安xx医院</h2>
<div>
<form action="doRegister.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="registerName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="registerPassword" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="sex" value="0" checked="checked" />男
<input type="radio" name="sex" value="1" />女</td>
</tr>
<tr>
<td>身份:</td>
<td><select name="princal">
<option>--请选择--</option>
<option value="doctor">院长</option>
<option value="nurse">副院长</option>
<option value="yuanzhang">专家</option>
<option value="admin">医生</option>
</select></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>
<%
if (session.getAttribute("errorsRegister") != null) {
out.print(session.getAttribute("errorsRegister"));
}
%>
</td>
</tr>
<tr>
<td><input type="submit" value="注册" />
</td>
<td><input type="reset" value="取消" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@page import="com.openlab.pojo.Employee,com.openlab.register.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doRegister.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/common.css">
</head>
<body>
<h2>注册页面</h2>
<%
request.setCharacterEncoding("UTF-8");
String registerName = request.getParameter("registerName");
String registerPassword = request.getParameter("registerPassword");
String sex = request.getParameter("sex");
String princal = request.getParameter("princal");
String email = request.getParameter("email");
Employee _employee = new Employee();
_employee.setRegisterName(registerName);
_employee.setRepassword(registerPassword);
_employee.setSex(Integer.parseInt(sex));
_employee.setPrical(princal);
_employee.setEmail(email);
DoRegister doregister = new DoRegister();
String result = doregister.doRegister(_employee);
if (result.equals("success")) {
response.sendRedirect("login.jsp");
} else {
session.setAttribute("errorsRegister", "注册失败...");
response.sendRedirect("register.jsp");
}
%>
</body>
</html>
效果实现图:
如有错误请大家指正。