源码如下
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>登陆页面</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>
<center><h1>欢迎来到管理登陆界面</h1></center>
<div align="center" style=" margin-top:100px;">
<form id="form" name="form" method="post" action="login.jsp">
<p>
<label >用户ID:
<input name="username" type="text" id="username" size="15" /></label>
</p>
<p>
<label>密 码:
<input name="password" type="password" id="password" size="15" /></label>
</p>
<label><input name="" type="submit" value="登陆" /></label>
</form>
</div>
</body>
</html>login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
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>登陆页面</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 userid = new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
if ( userid == "" || userid == null || userid.length()>20 || password == "" || password == null || password.length()>20 )
{
try
{
response.sendRedirect ( "wrong.jsp?wrongid=1" );
} catch ( Exception e )
{
}
}
else{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn =DriverManager.getConnection("jdbc:odbc:student");
stmt = conn.createStatement();
String sql="SELECT * FROM denglu where id="+userid+" and pw="+password;
if (stmt.execute(sql)) {
rs = stmt.getResultSet();
while(rs.next()){
out.println("登陆成功!<br />");
out.println("欢迎您:"+rs.getString("name"));
}
}
conn.close();
}
catch (SQLException ex){
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (InstantiationException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (IllegalAccessException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (ClassNotFoundException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
}
%>
</body>
</html>
wrong.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'wrong.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 n = request.getParameter("wrongid");
if(n.equals("1")){
out.println("1.您没输入ID或密码。<br />2.ID和密码不能超过20字符<br />");
out.println("<a href=\"index.jsp\">点此重新登陆</a>");
}
else if(n.equals("2")){
out.println("Sorry,登陆失败。<br />");
out.println("您输入的用户ID或密码不对哦。<br />");
out.println("<a href=\"index.jsp\">点此重新登陆</a>");
}
else{
out.println("你又在干坏事拉!");
}
%>
</body>
</html>Access数据库
编号 id pw
1 admin 123输入账号admin 密码123登不进去, 为什么求解
Sorry,登陆失败。
您输入的用户ID或密码不对哦。
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>登陆页面</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>
<center><h1>欢迎来到管理登陆界面</h1></center>
<div align="center" style=" margin-top:100px;">
<form id="form" name="form" method="post" action="login.jsp">
<p>
<label >用户ID:
<input name="username" type="text" id="username" size="15" /></label>
</p>
<p>
<label>密 码:
<input name="password" type="password" id="password" size="15" /></label>
</p>
<label><input name="" type="submit" value="登陆" /></label>
</form>
</div>
</body>
</html>login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
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>登陆页面</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 userid = new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
if ( userid == "" || userid == null || userid.length()>20 || password == "" || password == null || password.length()>20 )
{
try
{
response.sendRedirect ( "wrong.jsp?wrongid=1" );
} catch ( Exception e )
{
}
}
else{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn =DriverManager.getConnection("jdbc:odbc:student");
stmt = conn.createStatement();
String sql="SELECT * FROM denglu where id="+userid+" and pw="+password;
if (stmt.execute(sql)) {
rs = stmt.getResultSet();
while(rs.next()){
out.println("登陆成功!<br />");
out.println("欢迎您:"+rs.getString("name"));
}
}
conn.close();
}
catch (SQLException ex){
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (InstantiationException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (IllegalAccessException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (ClassNotFoundException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
}
%>
</body>
</html>
wrong.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'wrong.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 n = request.getParameter("wrongid");
if(n.equals("1")){
out.println("1.您没输入ID或密码。<br />2.ID和密码不能超过20字符<br />");
out.println("<a href=\"index.jsp\">点此重新登陆</a>");
}
else if(n.equals("2")){
out.println("Sorry,登陆失败。<br />");
out.println("您输入的用户ID或密码不对哦。<br />");
out.println("<a href=\"index.jsp\">点此重新登陆</a>");
}
else{
out.println("你又在干坏事拉!");
}
%>
</body>
</html>Access数据库
编号 id pw
1 admin 123输入账号admin 密码123登不进去, 为什么求解
Sorry,登陆失败。
您输入的用户ID或密码不对哦。
System.out.println("SQLException ") ;
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (InstantiationException e) {
System.out.println("InstantiationException ") ;
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (IllegalAccessException e) {
System.out.println("IllegalAccessException ") ;
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException ") ;
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
加上调试信息 ,看看你的报什么异常了 ,你都捕捉了异常,应该在后台或者日志中写入异常信息,否则怎么调试啊
catch(... e){
e.printStackTrace() ;
}上面我已经写了 ,你只用把2楼上的内容替换掉你的login.jsp中对应的,然后运行你的登录页面,然后看看后台打印了什么就可以了
catch (SQLException ex){
e.printStackTrace();
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (InstantiationException e) {
e.printStackTrace();
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (IllegalAccessException e) {
e.printStackTrace();
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (ClassNotFoundException e) {
e.printStackTrace();
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
53: while(rs.next()){
54: out.println("鐧婚檰鎴愬姛锛�br />");
55: out.println("娆㈣繋鎮細"+rs.getString("name"));
56: response
57: }
58: }
59: conn.close();
An error occurred at line: 56 in the jsp file: /login.jsp
Syntax error, insert ";" to complete Statement
53: while(rs.next()){
54: out.println("鐧婚檰鎴愬姛锛�br />");
55: out.println("娆㈣繋鎮細"+rs.getString("name"));
56: response
57: }
58: }
59: conn.close();
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:469)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.
首先:String userid = new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK");
这是你取的用户名,虽然你用的变量名是userid,但是他的值是用户名,然后你的SQL
String sql="SELECT * FROM denglu where id="+userid+" and pw="+password;
却是在用id和密码去匹配,你用用户名怎么去匹配id??能登录才有鬼
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
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>登陆页面</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 userid = new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
if ( userid == "" || userid == null || userid.length()>20 || password == "" || password == null || password.length()>20 )
{
try
{
response.sendRedirect ( "wrong.jsp?wrongid=1" );
} catch ( Exception e )
{
}
}
else{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn =DriverManager.getConnection("jdbc:odbc:student");
stmt = conn.createStatement();
String sql="SELECT * FROM denglu where id="+userid+" and pw="+password;
if (stmt.execute(sql)) {
rs = stmt.getResultSet();
while(rs.next()){
out.println("登陆成功!<br />");
out.println("欢迎您:"+rs.getString("name"));
}
}
conn.close();
}
catch (SQLException ex){
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (InstantiationException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (IllegalAccessException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
} catch (ClassNotFoundException e) {
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
}
%>
</body>
</html>
建议你把sql打印出来看一下,然后把打印出来的SQL贴到数据库里去执行一下
String sql="SELECT * FROM denglu where id='"+userid+"' and pw='"+password+"'";
把SQL改成上面的看看,id和password都是字符串
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
if ( userid == "" || userid == null || userid.length()>20 || password == "" || password == null || password.length()>20 )修改成 :String userid = new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
System.out.println("参数:" + userid + "-----" + password) ; //看看打印出来的是什么
if ( userid == null || userid == "" || userid.length()>20 || password == null || password == "" || password.length()>20 )
String sql="SELECT * FROM denglu where id='"+userid+"' and pw='"+password+"'";
下面加一句System.out.println(sql);
这个打印不是出现在页面上的,是日志文件里啊
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"),"GBK");
看下你取出的结果是啥样子的。我觉得可能和编码有关系,因为乱码了
1.但是如果输入admin 123的话还是提示您输入的密码不对,贴出控制台输出的错误信息
2.这样修改,你随便输入的用户名密码就不会白了
if(rs.next()){
out.println("登陆成功!<br />");
out.println("欢迎您:"+rs.getString("name"));
}else{
response.sendRedirect ( "wrong.jsp?wrongid=2" );
}
conn =DriverManager.getConnection("jdbc:odbc:student");
stmt = conn.createStatement();
String sql="SELECT * FROM denglu where id="+userid+" and pw="+password;中的
conn=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.MDB)};DBQ=access数据库文件存放的目录");