如何在java中将值从java类返回到Servlet

时间:2015-10-02 09:46:19

标签: java servlets

我有java类LoginValidation和Servlet Login,我将值从servlet传递给java类,但是没有得到返回值...从servlet到java类......

//普通的java类LoginValidation

public class LoginValidation {

    String userid="";
    String password="";
    String que="";
    Connection dbConnection = null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    String userid1="";
    String password1="";
    int pan1=0;

    public long valiDate(String userid ,String password){


        long flag = 0l; 
        this.password=password;
        this.userid=userid;

        dbConnection = JDBCConnection.getDBConnection();
        que="select * from shivu";
        try {
            pst = dbConnection.prepareStatement(que);
            rs=pst.executeQuery();

            while(rs.next()){
             userid1=rs.getString(3);
             password1=rs.getString(2);
             pan1=rs.getInt(8);

             if ((userid.equals(userid1)) && (password.equals(password1))){
                    flag = pan1;    

             }else{
                 flag = 0;

             }   
             }

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

        return flag;
    }
}

// Servlet登录

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub



        String userid=request.getParameter("userid");

        String password=request.getParameter("password");

        LoginValidation lv=new LoginValidation();

        System.out.println("control flow");

        long i=lv.valiDate(userid,password);

        System.out.println(i);

        if(i>=1){       

            System.out.println("control flow inside method call");

            HttpSession session = request.getSession();

            if (session != null)
                session.setAttribute("pan", i);

            response.sendRedirect("welcome.jsp");
        }
        else
        {

            System.out.println("Username or Password incorrect");
            response.sendRedirect("login1.jsp");
        }




    }

}

1 个答案:

答案 0 :(得分:0)

您可以打印并查看valiDate方法中的值。

        while(rs.next()){
         userid1=rs.getString(3);
         password1=rs.getString(2);
         pan1=rs.getInt(8);

         // print userid, userid1, password, password1, pan1

         if ((userid.equals(userid1)) && (password.equals(password1))){
                flag = pan1;    

         }else{
             flag = 0;

         }   
         }