先不考虑sql注入漏洞问题,代码如下:
package com.servlet;
import javax.servlet.http.*;
import java.sql.*;
public class LoginCl extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
String u=req.getParameter("username");
String p=req.getParameter("passwd");
//到oracle中验证
Class.forName("oracle.jdbc.driver.OracleDriver");
ct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myora1","scott","tiger");
ps=ct.prepareStatement("select * from users where username='"+u+"' and passwd='"+p+"'");
rs=ps.executeQuery();
if(rs.next()){
if(rs.getString(1).equals(p)){
//合法
//创建一个session,并放入一个数据
HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(20);
hs.setAttribute("haha", "ok");
res.sendRedirect("wel?username="+u+"&passwd="+p);
}else{
//密码错误
res.sendRedirect("login?error=2");
}
}else{
//用户名不存在
res.sendRedirect("login?error=1");
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req, res);
}
}我在oracle中建了一张users表
SQL> select * from users;USERNAME PASSWD
------------------------------ ------------------------------
sp 123
admin admin
feilong 123
lanzhou 123
为什么只有admin能成功登录 而其他的报密码错误。。
但是换成在sql2000中就能跑起来
新手学servlet,求解,测试半天了
在线等啊。。
package com.servlet;
import javax.servlet.http.*;
import java.sql.*;
public class LoginCl extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
String u=req.getParameter("username");
String p=req.getParameter("passwd");
//到oracle中验证
Class.forName("oracle.jdbc.driver.OracleDriver");
ct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myora1","scott","tiger");
ps=ct.prepareStatement("select * from users where username='"+u+"' and passwd='"+p+"'");
rs=ps.executeQuery();
if(rs.next()){
if(rs.getString(1).equals(p)){
//合法
//创建一个session,并放入一个数据
HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(20);
hs.setAttribute("haha", "ok");
res.sendRedirect("wel?username="+u+"&passwd="+p);
}else{
//密码错误
res.sendRedirect("login?error=2");
}
}else{
//用户名不存在
res.sendRedirect("login?error=1");
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req, res);
}
}我在oracle中建了一张users表
SQL> select * from users;USERNAME PASSWD
------------------------------ ------------------------------
sp 123
admin admin
feilong 123
lanzhou 123
为什么只有admin能成功登录 而其他的报密码错误。。
但是换成在sql2000中就能跑起来
新手学servlet,求解,测试半天了
在线等啊。。
package com.servlet;
import javax.servlet.http.*;
import java.sql.*;
public class LoginCl extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res){
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
String u=req.getParameter("username");
String p=req.getParameter("passwd");
//到oracle中验证
Class.forName("oracle.jdbc.driver.OracleDriver");
ct=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:myora1","scott","tiger");
ps=ct.prepareStatement("select * from users where username='"+u+"' and passwd='"+p+"'");
rs=ps.executeQuery();
if(rs.next()){
if(rs.getString(1).equals(p)){
//合法
//创建一个session,并放入一个数据
HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(20);
hs.setAttribute("haha", "ok");
res.sendRedirect("wel?username="+u+"&passwd="+p);
}else{
//密码错误
res.sendRedirect("login?error=2");
}
}else{
//用户名不存在
res.sendRedirect("login?error=1");
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req, res);
}
}貌似刚才发的不好看。。
ps=ct.prepareStatement("select * from users where username='"+u+"' and passwd='"+p+"'");
不该用+号,应该用||
但是还是没写出来。。
求解
String getString(int columnIndex)
throws SQLException以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。 参数:
columnIndex - 第一个列是 1,第二个列是 2,…… (所以你在index=1时admin的密码也是admin,so成功,而其他的都失败)
返回:
列值;如果值为 SQL NULL,则返回值为 null
抛出:
SQLException - 如果 columnIndex 无效;如果发生数据库访问错误或在已关闭的结果集上调用此方法
rs.getString("password")这样不容易把数据取错。
还有在操作ResultSet的时候一般用while循环,即使只有一条记录也如此:
while(rs.next()){....},这样既做了判断又移动了游标,一举两得呀。