一个简单的登陆页面实现
来源:互联网 发布:爱因斯坦厉害知乎 编辑:程序博客网 时间:2024/05/28 15:51
显示页面 index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
String username = (String)session.getAttribute("login");
if(session.getAttribute("login")==null){
response.sendRedirect("login.jsp");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>welcome <%=username%></h1>
</body>
</html>
登陆页面login.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>请登陆...</h1>
<form name="logonForm" action="loginaction.jsp" method="post">
<table border=0 cellpadding=5 cellspacing=0 width="100%" height="100%" >
<tr>
<td VALIGN="top" WIDTH="100%" class="login-background" >
<table border=0 cellpadding=0 cellspacing=0 width="65%" bgColor="#e8e8e8">
<tr>
<TD class="wpsLoginHeadText" noWrap>
欢迎,请输入您的信息。<BR><BR>
</TD>
</tr>
<tr>
<td valign=top width="33%" class="wpsLoginText" nowrap >
<br>
<label for="username" CLASS="noIndentLabel">用户标识:</label>
<BR>
<input TYPE="text" name="username" class="noIndentTextEntry" id="username"/>
<BR>
</td>
</tr>
<tr >
<td valign=top width="33%" class="wpsLoginText" nowrap >
<br>
<label for="password" CLASS="noIndentLabel">密码:</label>
<BR>
<input TYPE="password" name="password" class="noIndentTextEntry" id="password"/>
<BR>
<BR>
<BR>
</td>
</tr>
<tr>
<td valign=top class="wpsLoginText" nowrap >
<input TYPE="submit" name="action" class="buttons" id="other" VALUE="登录">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
loginaction.jsp页面
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8" import="com.yourcom.util.UsernameAndPassword,javax.servlet.http.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
UsernameAndPassword uad = new UsernameAndPassword();
boolean hasUser = uad.getUsername(username);
if(!hasUser){
%>
<script>
alert("没有这个用户");
window.history.go("-1");
</script>
<%
}else{
boolean hasUsernameAndPassword = uad.getUsernameAndPassword(username,password);
if(hasUsernameAndPassword){
session.setAttribute("login",username);
response.sendRedirect("index.jsp");
}else{
%>
<script>
alert("密码不正确!");
window.history.go("-1");
</script>
<%
}
}
%>
连接数据库DBConnection.java
public class DBConnection {
/** Creates a new instance of DBConnection */
public DBConnection() {
}
public Connection getConnection(){
Connection conn = null;
String username = "username";
String password = "password";
String url = "jdbc:oracle:thin:@localhost:1521:sid";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection(url,username,password);
} catch (SQLException ex) {
ex.printStackTrace();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return conn;
}
public static void main(String args[]){
DBConnection d = new DBConnection();
Connection conn = d.getConnection();
if(conn!= null){
System.out.println("get connection successful!");
}
}
}
判断登陆数据库的 类 UsernameAndPassword.java
public class UsernameAndPassword {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
/** Creates a new instance of UsernameAndPassword */
public UsernameAndPassword() {
}
public boolean getUsername(String username){
String sql = "select * from login where username='"+username+"'";
boolean hasUser = false;
DBConnection dbc = new DBConnection();
conn = dbc.getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs!=null&&rs.next())
hasUser = true;
} catch (SQLException ex) {
ex.printStackTrace();
}finally{
close();
}
return hasUser;
}
public boolean getUsernameAndPassword(String username,String password){
boolean hasUsernameAndPassword = false;
String sql = "select * from login where username = '"+username+"'and password = '"+password+"'";
DBConnection dbc = new DBConnection();
conn = dbc.getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs!=null&&rs.next()){
hasUsernameAndPassword = true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}finally{
close();
}
return hasUsernameAndPassword;
}
public void close(){
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null&&!conn.isClosed()){
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
- 一个简单的登陆页面实现
- 简单的实现页面登陆
- 超简单的登陆注册页面实现
- 简单的登陆页面
- 一个Android登陆的简单实现
- ado.net实现一个简单的登陆
- swift 实现一个登陆页面
- extJs+json实现的一个登陆验证的简单例子
- 一个简单的学生登陆系统的jsp实现_1
- 一个简单网易新闻页面的实现
- 实现一个简单的 页面划入效果
- 用django实现一个简单的页面
- Android 登陆页面 记住密码和自动登陆的简单实现
- 只用UILabel实现简单登陆页面
- struts2学习笔记(1) 实现一个简单的struts2登陆
- 用JAVA实现一个简单的登陆窗口
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 一个简单实现登陆控制的struts2框架
- Div层显示不能覆盖表单Select元素问题
- 如何定位"无法重现“的问题
- 买个闹钟
- 2007年最主流操作系统、数据库、协作办公、IDE
- JAVA连接数据库
- 一个简单的登陆页面实现
- JSP 参数传递显示乱码
- “白领工资标准”是炒作抑或是无聊之作?
- RS-232、RS-422|EIA232与RS-485|EIA485协议标准及应用概述
- WebSphere 在linux下的安装配置过程
- 排序算法归总
- RS-232、RS-422与RS-485串口标准简介
- C#中读写INI配置文件
- 按某一字段分组取最大(小)值所在行的数据(sql server)