jsp实现简单用户登录

来源:互联网 发布:淘宝网m 编辑:程序博客网 时间:2024/06/05 01:55

技术核心:jsp页面实现my sql数据库的链接和用户的简单登录。

页面代码:

Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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>用户登录页面</title>
</head>
<body>
<div align="center">
<font size=" 2" color="#FF6633">用户登录</font>
</div>
<form id="form1" name="form1" method="post" action="Checklogin.jsp"> 
用户名:
<label>
<input type="text" name="username" />
</label>
<p>
密码:
<label>
<input type="text" name="password" />
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</p>


</form>
</body>
</html>

Checklogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
  import="java.sql.*" 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>Insert title here</title>
</head>
<body>
<%
String username=new String(request.getParameter("username").getBytes


("ISO8859_1"),"GBK");
String password=new String(request.getParameter("password").getBytes


("ISO8859_1"),"GBK");
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/it";
// 数据库用户名
String usename = "root";
// 数据库密码
String psw = "123456";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,usename,psw);
// 判断 数据库连接是否为空
if(conn != null){
String sql="select * from admin where username='"+username+"' and password='"+ password + "'";
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){


response.sendRedirect("Login_success.jsp");
}
else
{
out.print("用户名或密码错误,请重新输入!");
%>
<a href="javascript:history.back()">返回</a>
<%
}

// 输出连接信息
//out.println("数据库连接成功!");
// 关闭数据库连接
conn.close();
}else{
// 输出连接信息
out.println("数据库连接失败!");

}
} catch (ClassNotFoundException e)
 {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}%></body>
</html>
0 0
原创粉丝点击