1

在我的项目中,用户通过提供他/她emailidpassword. 如果两者都匹配,则可以成功登录。如果不是,他将被重定向到UserHome.jsp页面。这是我的代码:

import getset.Getset;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

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 accessdb.Dao;

public class LoginAuthentication extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Authentication and Logging in The Registered User
        Getset g=new Getset();
        Dao dao=new Dao();
        String userid="";
        String fname="";
        //    PrintWriter pw=response.getWriter();
        String loginemail=request.getParameter("loginemail");
        String loginpassword=request.getParameter("loginpassword");
        if (loginemail.equals("") || 
            loginemail.equals(" ") || 
            loginpassword.equals("") || 
            loginpassword.equals(" "))

            response.sendRedirect("WelcomePage.jsp");

        g.setloginemail(loginemail);
        g.setloginpassword(loginpassword);
        try {
            ResultSet rs=dao.loginauthentication(g);
            while(rs.next())
            {
                String regemail=rs.getString("regemail");
                String regpassword=rs.getString("regpassword");
                if(loginemail.equals(regemail) && 
                   (loginpassword.equals(regpassword))==true)
                {
                    ResultSet rs1=dao.getnameid(g);
                    while(rs1.next())
                    {
                         userid=rs1.getString("USERID");
                         fname=rs1.getString("FNAME");
                    }
                    HttpSession session = request.getSession(true);
                    session.setAttribute("USERID", userid);
                    session.setAttribute("FNAME", fname);
                    response.sendRedirect("UserHome.jsp");
                    break;
                }
                else if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==false)
                {
                    response.sendRedirect("WelcomePage.jsp");
                    return;
                }
            }
        } 
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我试图涵盖登录过程中可能出现的所有情况。如果用户无法访问

  1. 两个字段都为空。
  2. email已填充(使用正确或不正确的数据),但未password填充。
  3. password已填充(使用正确或不正确的数据),但未emailid填充。

我试图覆盖但没有发生的区域是,如果任何字段填充了不正确的数据,则不要让用户访问。对于这一部分,我已经在代码中编写了:

 if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==true)
     //user accesses
 else 
     if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==false)
         //user cannot access

但我不知道为什么它没有显示预期的行为,并且当两个字段都填充了不正确的数据时,用户被重定向到一个空白页面!

补充:我也试过

if(loginemail.equals(regemail) && (loginpassword.equals(regpassword)))
   //User accesses
 else if(!loginemail.equals(regemail) || (!loginpassword.equals(regpassword)))
  //User cannot access

但这也无济于事!

4

3 回答 3

0

使用一个简单的

else

代替

else if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==false)

另外,请检查Java 运算符优先级信息

因为,你有loginemail.equals(regemail) && (loginpassword.equals(regpassword))==false,这似乎被视为:false && false == false- 这将返回假。

于 2012-07-27T12:07:51.280 回答
0

你得到一个空白页。这意味着if您的代码中没有任何条件正在执行以重定向到页面。

我没有得到你想要达到的目标。理想情况下,您应该只有两个条件:

  1. 在电子邮件和密码正确时重定向到 UsersHome。

  2. 如果其中任何一个不正确,请重定向到欢迎页面。

代码片段:

// Servlet instance variable
String redirectPage = "WelcomePage.jsp";

改变

if(loginemail.equals("")||loginemail.equals(" ")||loginpassword.equals("")||loginpassword.equals(" "))
        response.sendRedirect("WelcomePage.jsp");

    if(loginemail.equals("")||loginemail.equals(" ")||loginpassword.equals("")||loginpassword.equals(" "))
         redirectMyPage(response);
    // There should be only one record. So, If should be used.
    if(rs.next()) {
        //In my opinion there is no need of this. But you haven't mentioned your DAO part.
        // You should write DAO in such manner that if both email and password are correct, then
        // only return a record from the database.
        if(loginemail.equals(regemail) && (loginpassword.equals(regpassword))==true)
        {
            //Your other code ...
            HttpSession session = request.getSession(true);
            session.setAttribute("USERID", userid);
            session.setAttribute("FNAME", fname);
            redirectPage = UserHome.jsp";
        }
        // No need of else
    }
    redirectMyPage(response);
    //.. Other code
}

private void redirectMyPage(HttpResponse response){
    response.sendRedirect(redirectPage);
    return;
}
于 2012-07-27T12:09:26.370 回答
-1

经过长时间的尝试,我终于得到了我的工作代码。这里是:

import getset.Getset;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

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 accessdb.Dao;

public class LoginAuthentication extends HttpServlet {
private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Authentication and Logging in The Registered User
    Getset g=new Getset();
    Dao dao=new Dao();
    String userid="";
    String fname="";
//  PrintWriter pw=response.getWriter();
    String loginemail=request.getParameter("loginemail");
    String loginpassword=request.getParameter("loginpassword");
    if(loginemail.equals("")||loginemail.equals(" ")||loginpassword.equals("")||loginpassword.equals(" "))
    {
        response.sendRedirect("WelcomePage.jsp");
    }
    else{
    g.setloginemail(loginemail);
    g.setloginpassword(loginpassword);
    try {
        ResultSet rs=dao.loginauthentication(g);
        while(rs.next())   //Fetching all emails and passwords from user table
        {
            String regemail=rs.getString("regemail");
            String regpassword=rs.getString("regpassword");
            System.out.println(""+regemail);
            if(loginemail.equals(regemail) && (loginpassword.equals(regpassword)))
            {   
                System.out.println("55555");
                ResultSet rs1=dao.getnameid(g);
                while(rs1.next())   //GET USERID and name FROM NEWUSER TO USE AS PRIMARY KEY
                {
                     userid=rs1.getString("USERID");
                     fname=rs1.getString("FNAME");
                    System.out.println(""+userid);

                }

                HttpSession session = request.getSession(true);
                  session.setAttribute("USERID", userid);
                  session.setAttribute("FNAME", fname);
                response.sendRedirect("UserHome.jsp");
                break;
            }

        }

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}
}
}

谢谢大家,谁试图帮助我!

于 2012-07-27T13:28:38.390 回答