简单的登陆功能 oracle数据库 myeclipse建的工程 导入了struts 没有一个.java文件 用了4个JSP页面 index.jsp 登陆页面
ok.jsp 登入成功页面
error.jsp 失败页面
estimate.jsp 判断页面问题是到判断页面就不动了 我估计是没有像一般工程那样 建lib文件夹 加入classes12.jar
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'index.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>
<br>
<form name="loginform" method="post" action="COPYestimate.jsp">
<div align="center"></div>
<label></label>
<div align="center">
<p> 账 号:
<input type="text" name="username">
</p>
<p> 密 码:
<input type="password" name="password">
</p>
<label></label>
<div align="center">
<input name="Submit" type="submit" id="Submit" value="提交">
<input name="Submit1" type="reset" id="Submit1" value="重置">
</div>
</div>
<p>
<label></label>
<label></label>
</p>
</form>
</body>
</html>ok.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'ok.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>
欢迎你,<%=session.getAttribute("userName") %> <a href="index.jsp">退出</a>
</body>
</html>estimate.jsp
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%>
<%
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 'ok.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>
<%
String uname = (String)request.getParameter("username");
String psw = request.getParameter("password"); Connection conn = null;
String url = "jdbc:oracle:thin:@192.168.16.251:1521:thor";
String username = "aaaa";
String password = "bbbb";
String sql = "select username, password from spume0711 ";
// String [] str = new String [20];
try {
//创建oracle数据库驱动实例
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//获得与数据库的连接
conn = DriverManager.getConnection(url,username,password);
//创建执行语句对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
out.print("aaaaa");
String userName = rs.getString(1);
String passWord = rs.getString(2);
if(userName.equals(uname) && passWord.equals(psw)){
session.setAttribute("userName", uname);
response.sendRedirect("ok.jsp");
}else{
response.sendRedirect("error.jsp");
}
}catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
ok.jsp 登入成功页面
error.jsp 失败页面
estimate.jsp 判断页面问题是到判断页面就不动了 我估计是没有像一般工程那样 建lib文件夹 加入classes12.jar
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'index.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>
<br>
<form name="loginform" method="post" action="COPYestimate.jsp">
<div align="center"></div>
<label></label>
<div align="center">
<p> 账 号:
<input type="text" name="username">
</p>
<p> 密 码:
<input type="password" name="password">
</p>
<label></label>
<div align="center">
<input name="Submit" type="submit" id="Submit" value="提交">
<input name="Submit1" type="reset" id="Submit1" value="重置">
</div>
</div>
<p>
<label></label>
<label></label>
</p>
</form>
</body>
</html>ok.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'ok.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>
欢迎你,<%=session.getAttribute("userName") %> <a href="index.jsp">退出</a>
</body>
</html>estimate.jsp
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%>
<%
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 'ok.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>
<%
String uname = (String)request.getParameter("username");
String psw = request.getParameter("password"); Connection conn = null;
String url = "jdbc:oracle:thin:@192.168.16.251:1521:thor";
String username = "aaaa";
String password = "bbbb";
String sql = "select username, password from spume0711 ";
// String [] str = new String [20];
try {
//创建oracle数据库驱动实例
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//获得与数据库的连接
conn = DriverManager.getConnection(url,username,password);
//创建执行语句对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
out.print("aaaaa");
String userName = rs.getString(1);
String passWord = rs.getString(2);
if(userName.equals(uname) && passWord.equals(psw)){
session.setAttribute("userName", uname);
response.sendRedirect("ok.jsp");
}else{
response.sendRedirect("error.jsp");
}
}catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>
楼主【whlusha720】截止到2008-07-16 16:26:53的历史汇总数据(不包括此帖):
发帖的总数量:5 发帖的总分数:240 每贴平均分数:48
回帖的总数量:20 得分贴总数量:2 回帖的得分率:10%
结贴的总数量:5 结贴的总分数:240
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
out.print("aaaaa");
String userName = rs.getString(1);
String passWord = rs.getString(2);
打印的语句放这个位置就不能打印了
放上面一行就能打印出来
就是转到estimate.jsp 就不执行了 我估计也没有链接到数据库
WEB-INF/lib目录下 有导入struts时自动导入的一些jar包 我刚刚又导入了classes12.jar
数据库路径 是对的 我用其他工程测试过了 现在还是没找到原因 请大家帮忙 谢谢 !!
String uname = (String)request.getParameter("username");
String psw = request.getParameter("password");
把两个都打印一下,看看有没有得到传来的参数,
如果有,再看看是否成功从数据库里取出数据
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
out.println("get datas!");
String userName = rs.getString(1);
String passWord = rs.getString(2);
}
这一句有没写错啊, if(userName.equals(username) && passWord.equals(password)){
是不是应该这样写啊,鄙人也是自己的看法,也在学的过程中,有说错,别介意
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//获得与数据库的连接
conn = DriverManager.getConnection(url,username,password);
//创建执行语句对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String userName = rs.getString(1);
String passWord = rs.getString(2);
经测试 可以接收到index.jsp页面传来的参数
发现这一行是分水岭 ResultSet rs = stmt.executeQuery
这一行上面的代码可以执行 下面就不行了
这是可以打印出来的
这是可以打印出来的 不过要放在 ResultSet rs = stmt.executeQuery(sql); 这一行上面才行PS:为什么不能编辑自己发的帖子啊 ?
2。 怀疑记录太多,挂在那了,可以根据用户输入的用户名构造一下sql语句。
这张表我随便建的 只有2条记录 不存在记录多卡机的情况 我估计是6楼的朋友说的情况connection创建没有成功,原因可能是没找到驱动类
又在工程里导入了ojdbc14.jar 情况还是一样
ResultSet rs = stmt.executeQuery(sql); 上面的代码可以执行 下面的都不执行!
看有没有记录。
这行代码以下都无效 怎么打印System.out.println(rs.next()); 啊 ?? rs都没有