1.添加jar包。
建立动态java项目,在Web-INF文件夹下的lib文件夹里添加jar包。
区别:java中添加的jar包需要添加构建路径,而javaWeb中添加的jar不需要构建路径,自动添加。
2.创建请求页面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="Check.jsp" method="post">
卡号:<input type="text" name="cardid"><br>
密码:<input type="password" name="password"><br>
按钮:<input type="submit" value="登录">
</form>
</body>
</html>
3.创建接收页面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
//接收验证
String cardid = request.getParameter("cardid");
String password = request.getParameter("password");
//数据验证
if(cardid.equals("")||password.equals(""))
{
out.write("请正确登录系统");
}
else if(cardid==null||password==null)
{
out.write("请正确登录系统");
}
else
{
out.write(cardid);
out.write(password); //连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test0816", "laoer123");
PreparedStatement ps = conn.prepareStatement("select * from t_balance where card_id=? and password=? ");
ps.setString(1, cardid);
ps.setString(2,password);
ResultSet rs =ps.executeQuery();
//数据验证
if(rs.next())
{
out.write("用户名和密码正确");
}
else
{
out.write("用户名或密码错误");
}
rs.close();
ps.close();
conn.close();
} %>
</body>
</html>
4.通过连接池连接数据库
(1)添加jar包,同上。
(2)在JavaResources - src- 创建java包 -创建类 ,并将配置文件放在src下
package com.hanqi.web; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource;
//单例模式
public class CardDAO {
//构建连接池对象并配置
private ComboPooledDataSource cpds =new ComboPooledDataSource("helloc3p0"); public boolean checklogin(String cardid,String password)
{
boolean rtn =false;
try {
Connection conn = cpds.getConnection();
PreparedStatement ps = conn.prepareStatement("select * from t_balance where card_id=? and password=? ");
ps.setString(1, cardid);
ps.setString(2,password);
ResultSet rs =ps.executeQuery(); rtn=rs.next();
rs.close();
ps.close(); conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rtn;
} }
请求信息页面
<%@ 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>Insert title here</title>
</head>
<body>
<form action="Check2.jsp" method="post">
卡号:<input type="text" name="cardid"><br>
密码:<input type="password" name="password"><br>
按钮:<input type="submit" value="登录">
</form>
</body>
</html>
接收信息的页面
<%@page import="com.hanqi.web.CardDAO"%>
<%@ 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>Insert title here</title>
</head>
<body>
<%
// 设置不缓存页面
response.setHeader("Cache-Control", "no-cache");
//定时跳转
//response.setHeader("refresh", "2;URL=http://www.baidu.com");
// String cardid = request.getParameter("cardid");
String password =request.getParameter("password");
if(cardid==null||password==null||
cardid.equals("")||password.equals(""))
{
out.write("请正确登录");
}
else
{
//检查登录信息
CardDAO cd = new CardDAO();
if(cd.checklogin(cardid, password))
{
//out.write("登录成功!"); response.getWriter().write("验证成功!");
//页面跳转
//response.sendRedirect("Main.jsp");
response.sendRedirect("http://www.baidu.com");
}
else
{
out.write("登录失败!");
//跳回登录页面
response.setHeader("refresh", "2;URL=loginin.jsp");
}
} %> </body>
</html>
附原本的图,不是以上程序