String sName = request.getParameter("userName");
String pwd = request.getParameter("password");
//连接mdb数据库,验证用户名密码是否正确
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print("driver:"+e);
}
try{
String url="jdbc:odbc:jspdata";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String sql = "select * from userTable where userName='"+sName +"' and userPwd = '"+pwd+"'";
ResultSet rs = stmt.executeQuery(sql);
if(!rs.next()){
//查不出结果则返回到登录页面
response.sendRedirect("index.jsp");
}
else{
//将用户名记录到session中
session.setAttribute("userName", sName);
//登录主页面
request.getRequestDispatcher("main.jsp").forward(request, response);
}
}
catch(Exception e){
out.print(e);
}
String pwd = request.getParameter("password");
//连接mdb数据库,验证用户名密码是否正确
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print("driver:"+e);
}
try{
String url="jdbc:odbc:jspdata";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String sql = "select * from userTable where userName='"+sName +"' and userPwd = '"+pwd+"'";
ResultSet rs = stmt.executeQuery(sql);
if(!rs.next()){
//查不出结果则返回到登录页面
response.sendRedirect("index.jsp");
}
else{
//将用户名记录到session中
session.setAttribute("userName", sName);
//登录主页面
request.getRequestDispatcher("main.jsp").forward(request, response);
}
}
catch(Exception e){
out.print(e);
}
本文详细阐述了如何通过Java的HTTP请求参数获取用户名和密码,并利用ODBC驱动与Mdb数据库建立连接,验证用户登录信息的有效性。如果验证失败,则会重定向至登录页面;反之,将用户名记录至session并跳转至主页面。

703

被折叠的 条评论
为什么被折叠?



