String uid=request.getParameter("username"); String pwd=request.getParameter("pwd"); String cx=new String(request.getParameter("cx").getBytes("8859_1")); String sql=""; if(cx.equals("普通用户")) { sql="select * from yonghuxinxi where yonghuming='"+uid+"' and mima='"+pwd+"'"; } else { sql="select * from allusers where username='"+uid+"' and pwd='"+pwd+"'"; } ResultSet RS_result=connDbBean.executeQuery(sql); if(!RS_result.next()) { out.print("<script>alert('你输入的用户不存在或密码错误,请重新登录!');window.history.go(-1);</script>");} else{ session.setAttribute("username",uid); if(cx.equals("普通用户")) { session.setAttribute("cx","普通用户"); } else { session.setAttribute("cx",RS_result.getString("cx")); } response.sendRedirect("main.jsp"); } RS_result.close();请给这段代码加上注释
时间: 2023-07-19 08:58:24 浏览: 154
// 获取前端传递的参数:用户名、密码、用户类型
String uid = request.getParameter("username");
String pwd = request.getParameter("pwd");
String cx = new String(request.getParameter("cx").getBytes("8859_1"));
// 定义 SQL 查询语句
String sql = "";
if(cx.equals("普通用户")) {
// 如果是普通用户,则在 yonghuxinxi 表中查询用户名和密码
sql = "select * from yonghuxinxi where yonghuming='" + uid + "' and mima='" + pwd + "'";
} else {
// 如果是其他类型用户,则在 allusers 表中查询用户名和密码
sql = "select * from allusers where username='" + uid + "' and pwd='" + pwd + "'";
}
// 执行 SQL 查询语句
ResultSet RS_result = connDbBean.executeQuery(sql);
if(!RS_result.next()) {
// 如果查询结果为空,则输出错误提示信息,并返回上一页
out.print("<script>alert('你输入的用户不存在或密码错误,请重新登录!');window.history.go(-1);</script>");
} else {
// 如果查询结果不为空,则设置 session,并跳转到 main.jsp 页面
session.setAttribute("username", uid);
if(cx.equals("普通用户")) {
session.setAttribute("cx", "普通用户");
} else {
session.setAttribute("cx", RS_result.getString("cx"));
}
response.sendRedirect("main.jsp");
}
// 关闭 ResultSet 对象
RS_result.close();
阅读全文