Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.IllegalStateException: Response already committed

2700980Feb 15 2015 — edited Feb 15 2015

I am making an adf application where i am authenticating user and redirecting him to the application page from the login page. following are some points:

*     If user lands on the login page and if the session is available, he will be redirected to the application page.

*     if user lands on the application page and if the session is null, he will be redirected to the login page.

*     I am using beforePhase property of login page's f:view.


Problem:

When i authenticate the user successfully i am redirecting him to the application page and here all works fine. but after it if i open the login page again i should be redirected to application page as session is no null but i am not going to the application page instead getting this error

java.lang.IllegalStateException: Response already committed


Here is the code:

beforePhase method that runs when login page gets load.


public void beforePhase(PhaseEvent phaseEvent) {
    // Add event code here...
    if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {
        System.out.println("It's before phase.");

        fctx = FacesContext.getCurrentInstance();
        ec = fctx.getExternalContext();
        HttpSession userSession = (HttpSession)ec.getSession(true);
        if (userSession.getAttribute("user_id") != null) {
            System.out.println("Take user to the application" +
                               userSession.getAttribute("user_id"));
            response = (HttpServletResponse)ec.getResponse();
            request = (HttpServletRequest)ec.getRequest();

            try {
                response.sendRedirect("http://127.0.0.1:7101/EmployeeApraisalApplication-ViewController-context-root/faces/emp_apraise_form.jspx");
               
              
                return;                                                                                                                                            
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                fctx.responseComplete();
            }
        }
    }
}

Method that gets called when user presses login button


protected void auth(String username,String password) throws Exception{
    //LOADING THE DRIVERS TO CONNECT TO ORACLE DATABASE
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //CREATING THE CONNECTION
    connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.30:1526:SID", "apps","password");
 
    //IF CONNECTED SUCCESSFULLY
    if (connection != null) {
        String selectSQL = "SELECT USER_ID, USER_NAME,user_email FROM xx_emp_ap_users where upper(user_name) = ? and upper(user_password) = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
        preparedStatement.setString(1, username.toUpperCase());
        preparedStatement.setString(2, password.toUpperCase());
        ResultSet rs = preparedStatement.executeQuery();
        if (rs.next()) {
            System.out.println("Authentication Success.");
            String userName = rs.getString("user_name");
            String userID = rs.getString("user_id");
            String userEmail = rs.getString("user_email");
            createUserSession(userName,userID,userEmail);
            try {
                response.sendRedirect("http://127.0.0.1:7101/EmployeeApraisalApplication-ViewController-context-root/faces/emp_apraise_form.jspx");
                return;                                                                                                                                           
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                fctx.responseComplete();
            }
          
        }else{
            System.out.println("Failed.");
        }
    }
}

private void createUserSession(String userName, String userID,
                               String userEmail) {
  
    HttpSession userSession = (HttpSession)ec.getSession(true);
    userSession.setAttribute("user_name", userName);
    userSession.setAttribute("user_id", userID);
    userSession.setAttribute("user_email", userEmail);
}

I am using JDEV 11.1.1.7

-Thanks



Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 15 2015
Added on Feb 15 2015
1 comment
1,233 views