输出的结果就是直接跳到else里面的那个页面里面去
解决方案 »
- extjs groupinggrid
- 关于用JAVA&JSP做一个WEB语音聊天程序的问题
- io实现将java语句转换成xml文件??
- FileInputStream 文件拒绝访问问题
- 在一个JSP中如何操作另外一个JSP中的控件!!!急!!!
- 十万火急!!求一个简单的hibernate 例子!(在线等)
- 关于向已存在的excel文件中添加和修改内容
- 如何使用 jcombobox ?使其有显示值,绑定值,在线等待,谢谢大家了
- 请高手们阐述html,jsp,javascript之间的关系
- Eclipse和Tomcat的问题,高分!!!
- jsp include问题
- String类型日期转换成date类型日期,再提取间隔
{
response.sendRedirect("../main.jsp");
}
else
{
response.sendRedirect("../err.jsp");
}
}
}
问题是为什么执行的时候只执行else而不执行if里面的条件呢?
=======================================================================那就是你的if(objdbc.isUsers(name, pass))一直是else啊
这是程序执行的规则肯定不会错的
* 本类完成数据库连接管理
* 调用时直接调用该类的静态变量来获取该类实例
*/public class ConnectionManagerBean { static Connection objConn = null;
private static ConnectionManagerBean me = new ConnectionManagerBean(); public ConnectionManagerBean() {
} public static ConnectionManagerBean newInstance() {
return me;
} public static Connection getConnection()
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
objConn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb","sa","sa");
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//objConn = DriverManager.getConnection("jdbc:odbc:mydb");
} catch (Exception e) {
System.out.print(e.toString());
}
return objConn; }
}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class checkusersrv extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
//从会话中得到原来生成的验证码
HttpSession session = request.getSession();
String randSource = (String)session.getAttribute("rand");
session.removeAttribute("rand");
//得到表单数据
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String rand = request.getParameter("rand");
String userSf = request.getParameter("userSf");
//验证
if(!rand.equals(randSource))
{
response.sendRedirect("Fail.jsp?error=1");
}
else
{
//个人用户
if(userSf.equals("2")){
UserinfoBean objUserinfoBean = new UserinfoBean();
objUserinfoBean.setUserName(userName);
objUserinfoBean.setPassword(password);
if (objUserinfoBean.loginValidate()) {
session.setAttribute("userName", userName);
response.sendRedirect("Welcome.jsp?sf=2");
}
else {
response.sendRedirect("Fail.jsp?error=2");
}
}
//企业用户
else
{
qyuserinfoBean objqyuserinfoBean = new qyuserinfoBean();
objqyuserinfoBean.setQyuserName(userName);
objqyuserinfoBean.setPassword(password);
if(objqyuserinfoBean.loginValidate()){
session.setAttribute("userName", userName);
response.sendRedirect("Welcome.jsp?sf=1");
}
else {
response.sendRedirect("Fail.jsp?error=2");
}
}
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
* 封装Userinfo表中的列,提供get/set方法以及验证用户合法性的方法
*/
public class UserinfoBean {
private int userID;
private String userName;
private String password;
Connection conn = null;
ConnectionManagerBean objConnManager = ConnectionManagerBean.newInstance(); public UserinfoBean() {
} public void setUserID(int userID) {
this.userID = userID;
} public void setUserName(String userName) {
this.userName = userName;
} public void setPassword(String password) {
this.password = password;
} public int getUserID() {
return userID;
} public String getUserName() {
return userName;
} public String getPassword() {
return password;
} public boolean loginValidate() {
try {
Connection conn = objConnManager.getConnection();
String sqlStr = "select * from userinfo where userName=? and password=?";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setString(1, this.getUserName());
pstmt.setString(2, this.getPassword());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
else {
return false;
}
}
catch (SQLException ex) {
System.out.println("数据库操作失败!"+ex.getMessage());
return false;
}
finally {
try {
if (conn != null)
conn.close();
}
catch (SQLException sqlex) {
System.out.println("关闭数据库连接失败!"+sqlex.getMessage());
}
}
}
}package login;import java.sql.*;/*
* 封装qyuserinfo表中的列,提供get/set方法以及验证用户合法性的方法
*/public class qyuserinfoBean { private int qyuserID;
private String qyuserName;
private String password;
Connection conn = null;
ConnectionManagerBean objConnManager = ConnectionManagerBean.newInstance();
public qyuserinfoBean() {
} public int getQyuserID() {
return qyuserID;
} public void setQyuserID(int qyuserID) {
this.qyuserID = qyuserID;
} public String getQyuserName() {
return qyuserName;
} public void setQyuserName(String qyuserName) {
this.qyuserName = qyuserName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public boolean loginValidate() {
try {
Connection conn = objConnManager.getConnection();
String sqlStr = "select * from qyuserinfo where qyuserName=? and password=?";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setString(1, this.getQyuserName());
pstmt.setString(2, this.getPassword());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
else {
return false;
}
}
catch (SQLException ex) {
System.out.println("数据库操作失败!" + ex.getMessage());
return false;
}
finally {
try {
if (conn != null)
conn.close();
}
catch (SQLException sqlex) {
System.out.println("关闭数据库连接失败!" + sqlex.getMessage());
}
}
}}
这个方法返回的一直都是false,所以下面的页面调用会出错。
改成这样:
public boolean isUsers(String name,String pass)
{
try {
p=con.prepareStatement("select * from bbsusers where uname=? and upassword=?");
p.setString(1,name);
p.setString(2,pass);
r=p.executeQuery();
System.out.print(name);
System.out.print(pass);
if(r.next())
{
return true;
}else{
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}