关于JSP编写登陆界面的问题 10
小弟是JSP新手,最近编写了一个登陆界面,前台输入用户名和密码,与数据库中的数据做对比,如果匹配,则在SERVLET中跳转。程序中不知出现了什么问题,但是输入的密码和数据...
小弟是JSP 新手,最近编写了一个登陆界面,前台输入用户名和密码,与数据库中的数据做对比,如果匹配,则在SERVLET中跳转。
程序中不知出现了什么问题,但是输入的密码和数据库中的值始终不匹配
SERVLET代码如下
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import biz.AdminBiz;
@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
this.doService(request, response);
}
public void doService(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String path = request.getParameter("path");
if ("login".equals(path)) {
this.login(request, response);
}
}
public void login(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String toUrl = "";
String strError = "";
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name =(String)request.getParameter("name").trim() ;
String pass =(String)request.getParameter("pass").trim();
String rand =(String) request.getParameter("rand").trim();
HttpSession session = request.getSession();
String rand2 = (String) session.getAttribute("rand");
if (rand.equalsIgnoreCase(rand2)) {
String name=AdminBiz.getInstance().checkAdmin(name,pass).getName();
String pass=AdminBiz.getInstance().checkAdmin(name,pass).getPass();
if(name.equals(name)&& pass.equals(pass)) {
toUrl = "Query.jsp";
}else {
strError = "提示:用户名或密码有误!";
toUrl = "Login.jsp";
}
}else{
strError="提示:验证码错误!";
toUrl="Login.jsp";
}
request.setAttribute("error", strError);
request.getRequestDispatcher(toUrl).forward(request, response);
}
public void init() throws ServletException {
}
public void destroy() {
}
}
关于数据库连接方面小弟做过测试,基本没有问题。
能否请高手帮忙找出程序中的问题
另附上相关类代码:
package biz;
import beans.Admin;
import db2.ControlDB;
public class AdminBiz {
private static AdminBiz adminbiz = null;
private ControlDB control = null;
private AdminBiz() {
control = ControlDB.getInstance();
}
public static AdminBiz getInstance() {
if (adminbiz == null) {
adminbiz = new AdminBiz();
}
return adminbiz;
}
public Admin checkAdmin(String username, String userpass) {
Admin admin = new Admin();
String sql = "SELECT * FROM ADMIN WHERE NAME='"+username+"' and PASS='"+userpass+"'";
try {
admin = control.checkAdmin(sql);
} catch (Exception e) {
e.printStackTrace();
}
return admin;
}
} 展开
程序中不知出现了什么问题,但是输入的密码和数据库中的值始终不匹配
SERVLET代码如下
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import biz.AdminBiz;
@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
this.doService(request, response);
}
public void doService(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String path = request.getParameter("path");
if ("login".equals(path)) {
this.login(request, response);
}
}
public void login(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String toUrl = "";
String strError = "";
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name =(String)request.getParameter("name").trim() ;
String pass =(String)request.getParameter("pass").trim();
String rand =(String) request.getParameter("rand").trim();
HttpSession session = request.getSession();
String rand2 = (String) session.getAttribute("rand");
if (rand.equalsIgnoreCase(rand2)) {
String name=AdminBiz.getInstance().checkAdmin(name,pass).getName();
String pass=AdminBiz.getInstance().checkAdmin(name,pass).getPass();
if(name.equals(name)&& pass.equals(pass)) {
toUrl = "Query.jsp";
}else {
strError = "提示:用户名或密码有误!";
toUrl = "Login.jsp";
}
}else{
strError="提示:验证码错误!";
toUrl="Login.jsp";
}
request.setAttribute("error", strError);
request.getRequestDispatcher(toUrl).forward(request, response);
}
public void init() throws ServletException {
}
public void destroy() {
}
}
关于数据库连接方面小弟做过测试,基本没有问题。
能否请高手帮忙找出程序中的问题
另附上相关类代码:
package biz;
import beans.Admin;
import db2.ControlDB;
public class AdminBiz {
private static AdminBiz adminbiz = null;
private ControlDB control = null;
private AdminBiz() {
control = ControlDB.getInstance();
}
public static AdminBiz getInstance() {
if (adminbiz == null) {
adminbiz = new AdminBiz();
}
return adminbiz;
}
public Admin checkAdmin(String username, String userpass) {
Admin admin = new Admin();
String sql = "SELECT * FROM ADMIN WHERE NAME='"+username+"' and PASS='"+userpass+"'";
try {
admin = control.checkAdmin(sql);
} catch (Exception e) {
e.printStackTrace();
}
return admin;
}
} 展开
4个回答
展开全部
你没有把AdminBiz的代码贴出,也没办法看出什么问题,我建议多用用打印语句,先看看Servlet有没有得到用户名和密码,再看看数据库那边为什么没有查询到记录,是不是输错了,祝你好运!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我怎么没看到
String name=AdminBiz.getInstance().checkAdmin(name,pass).getName();
String pass=AdminBiz.getInstance().checkAdmin(name,pass).getPass();
这个里面的getName();和getPass();方法?
你可以debug模式 将断点打在这两句前面,这样问题就清楚了!
String name=AdminBiz.getInstance().checkAdmin(name,pass).getName();
String pass=AdminBiz.getInstance().checkAdmin(name,pass).getPass();
这个里面的getName();和getPass();方法?
你可以debug模式 将断点打在这两句前面,这样问题就清楚了!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用MYECLIPSE,打断点debug模式,看看哪个数据不对就对了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
回二楼:getName()和getPass()肯定是放在POJO beans.Admin中了....
程序中也没看出什么问题,估计是表单字段名写错了
程序中也没看出什么问题,估计是表单字段名写错了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询