Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6746279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:18:14+00:00 2026-05-26T12:18:14+00:00

In my project, I prohibit a user every page only if he is already

  • 0

In my project, I prohibit a user every page only if he is already logged on. That is why I wrote the below code. When I type in browser, for example, http://localhost:8080/JSP1/Students, I come to the login.jsp page. But after I input the loginid and password, only blank page http://localhost:8080/JSP1/Logged appears and GlassFish says there is an exception in

if (userPath.equals("/Students")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
        requestDispatcher.forward(request, response);
    }

java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed

Complete code for doGet and doPost:

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

     HttpSession ses = request.getSession();
     String login = (String)ses.getAttribute("login");
     String password = (String)ses.getAttribute("password");
     if ((login==null)|(password==null)){
         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
         requestDispatcher.forward(request, response);
     } 

     //Now we think that we are successfully logged in

    String userPath = request.getServletPath();
   // System.out.println(userPath);

    if (userPath.equals("/Login")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
        requestDispatcher.forward(request, response);
    }

    if (userPath.equals("/Students")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
        requestDispatcher.forward(request, response);
    }

     if (userPath.equals("/Student")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
        requestDispatcher.forward(request, response);
    }

     if (userPath.equals("/StudentEdit")){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
        requestDispatcher.forward(request, response);
    }

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
  //  processRequest(request, response);
    PrintWriter out = response.getWriter();

    String userPath = request.getServletPath();
    System.out.println(userPath);

     if (request.getRequestURI().equals("/Logged")){
            String Login = request.getParameter("login");
            String Password = request.getParameter("password");
            request.getSession().setAttribute("login", Login);
            request.getSession().setAttribute("password", Password);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
            requestDispatcher.forward(request, response);

        }

    if (userPath.equals("/addStudent")) {

       //     System.out.println(request.getContextPath());
            String Name = request.getParameter("name");
            String Surname = request.getParameter("surname");
            String Login = request.getParameter("login");
            String Password = request.getParameter("password");
            Student student = new Student(Name,Surname,Login,Password);
            if (student != null) {
                dao.insertStudent(student);
            } else {
                System.out.println("Not valid parameter!!!");
            }
             RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
             requestDispatcher.forward(request, response);
        }

    if (request.getRequestURI().equals("/Edit")) {
            System.out.println("We work with students!!!");
            String delete = request.getParameter("Add_new_student");
            if (delete != null){
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Student.jspx");
                requestDispatcher.forward(request, response);
            }

            Enumeration parameters = request.getParameterNames();
            while (parameters.hasMoreElements()) {
                String parameterName = (String) parameters.nextElement();
                String parameterValue = request.getParameter(parameterName);
                String norder = parameterName.substring(parameterName.indexOf("_")+1);

                ArrayList<Student> curStudents = dao.getAllStudents();
                int norderint = Integer.parseInt(norder);
                Student studentToWork = curStudents.get(norderint);

                String actionToDo = parameterName.substring(0, parameterName.indexOf("_"));

                if (actionToDo.equals("Edit")){

                RequestDispatcher requestDispatcher = request.getRequestDispatcher("/StudentEdit.jsp");
                ServletContext cont = request.getServletContext();
                cont.setAttribute("studentToEdit", studentToWork);
                requestDispatcher.forward(request, response);
                } else {
                    boolean attemp = dao.deleteStudent(studentToWork);
                    if (attemp){
                         RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Students.jsp");
                         requestDispatcher.forward(request, response);
                    } else {
                        out.println("Unsuccessfull attemp to delete a Student");
                    }
                }
            }
        }

      if (userPath.equals("/EditStudent")){
            System.out.println("We work with StudentEdit!");
            Student studentToEdit = (Student)request.getSession().getAttribute("studentToEdit");
            String newName = request.getParameter("name");
            String newSurname = request.getParameter("surname");
            String newLogin = request.getParameter("login");
            String newPassword = request.getParameter("password");
            Student newStudent = new Student(newName, newSurname,newLogin,newPassword);
            boolean update = dao.updateStudent(studentToEdit, newStudent);
            if (update){
            out.println("<p>You have successfully edited a Student=" + studentToEdit.toString() + " to Student="+ newStudent.toString());
            } else {
                 out.println("<p>Unsuccessful attempt to edit!</p>" );
            }
      }

}

The login.jsp is simple:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form action="/JSP1/Logged" method="POST">
    <table>           
    <tr>
        <td>Login:</td>
        <td><input type="text" name="login" value=""/>  </td>   
    </tr>
    <tr>
         <td>Password </td>
         <td><input type="password" name="password"/> ></td>

    </tr>
     <tr>
         <td><input type="submit" name="OK" value="OK" /> </td>
    </tr>
    </table>
</form>
</body>

I can’t figure out what’s happening.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:18:14+00:00Added an answer on May 26, 2026 at 12:18 pm

    You aren’t returning after the forward when the login and/or password is not been supplied. It’s a common misconception among starters that the forward() method magically terminates the code execution and jumps out of the method somehow. This is thus not true. You have to return from the method and stop the execution of the remnant of the code yourself.

    You need to either add a return;

    if ((login==null)|(password==null)){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
        requestDispatcher.forward(request, response);
        return;
    } 
    

    Or to add an else

    if ((login==null)|(password==null)){
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/login.jsp");
        requestDispatcher.forward(request, response);
    } else {
        // Now we think that we are successfully logged in.
    
        // Yes, that above comment is now finally true.
        // Put your bunch of non-DRY if-else code here.
    }
    

    See also:

    • java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every project invariably needs some type of reporting functionality. From a foreach loop in
Project is C#. So I have a bunch of multithreaded code that is designed
project type is MVC2. Let say that i have page1. after success it write
Project type: Asp MVC 2/NHibernate/C# Problem If you have an edit page in an
Project Requirements: I need to build a web service that will receive chunks of
Project : ASP.NET 3.5 Server : SQL Server 2008 One of the page in
Project Lombok makes it trivial to implement the boilerplate code in the classes. Is
Project (id) Permission (project_id, user_id) When user's create a project, I want to ensure
I have a project with autotools: automake, autoconf. I want to prohibit make from
Project: I am having a button that toggles the column length from 3 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.