Results 1 to 3 of 3
  1. #1
    nakta is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Post Exception: Cannot forward after response has been committed

    I have been working on this website for my university project and I was trying to create a register servlet.

    The HTML works just fine, but instead of the desired message I get
    "Exception: Cannot forward after response has been committed"

    Java Code:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    
    public class Servlet_register extends HttpServlet {
    
       public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException {
    
        response.setContentType("text/html; charset=ISO-8859-7");
        PrintWriter out = new PrintWriter(response.getWriter(), true);
    
     		    String username=request.getParameter("username");
    			String password=request.getParameter("password");
    			String email=request.getParameter("email");
    			String style=request.getParameter("style");
    			String surname=request.getParameter("surname");
    			String name=request.getParameter("name");
    			String age=request.getParameter("age");
    			String sex=request.getParameter("sex");
    			String country=request.getParameter("country");
    			String city=request.getParameter("city");
    		
    
        RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher("/servlet/ErrorHandlerServlet");
    
        try {
    
        			username=new String(username.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			password=new String(password.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			email=new String(email.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			style=new String(style.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			surname=new String(surname.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			name=new String(name.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			country=new String(country.getBytes("ISO-8859-1"), "ISO-8859-7");
    	 			city=new String(city.getBytes("ISO-8859-1"), "ISO-8859-7");
    
    				out.println("<html>");
    	  			out.println("<title>Artelier - Your Art Community</title>");
    	  			out.println("<style type='text/css'>");
    	  			out.println("<!--#wrapper {");
    	  			out.println("	background-color: #f7f7f7;");
    	  			out.println("	width: 800px;");
    	  			out.println("	margin-right: auto;");
    	  			out.println("	margin-left: auto;");
    	  			out.println("	text-align: justify;");
    	  			out.println("	border-top-color: #f7f7f7;");
    	  			out.println("	border-right-color: #f7f7f7;");
    	  			out.println("	border-bottom-color: #f7f7f7;");
    	  			out.println("	border-left-color: #f7f7f7;");
    	  			out.println("}");
    	  			out.println("* {");
    	  			out.println("	margin: 0px;");
    	  			out.println("}");
    	  			out.println("#navigation {");
    	  			out.println("	background-color: #DBDBB7;");
    	  			out.println("	width: 800px;");
    	  			out.println("	text-align: right;");
    	  			out.println("	float: left;");
    	  			out.println("	height: 20px;");
    	  			out.println("	font-family: Arial, Helvetica, sans-serif;");
    	  			out.println("	font-size: 12px;");
    	  			out.println("}");
    	  			out.println(".style1 {color: #000;}");
    	  			out.println(".style1 a:link {color:#000;}");
    	  			out.println(".style1 a:visited {color:#000;}");
    	  			out.println("			out.println('.style1 a:active {color:#000;}");
    	  			out.println("");
    	  			out.println("#wrapper #navigation #position {");
    	  			out.println("	margin-right: 400px;");
    	  			out.println("}");
    	  			out.println("");
    	  			out.println("#wrapper #bodyArea #left {");
    	  			out.println("	width: 550px;");
    	  			out.println("	float: left;");
    	  			out.println("	font-family: Arial, Helvetica, sans-serif;");
    	  			out.println("	font-size: 14px;");
    	  			out.println("	text-align: left;");
    	  			out.println("}");
    	  			out.println("#wrapper #bodyArea #right {");
    	  			out.println("	width: 250px;");
    	  			out.println("	float: right;");
    	  			out.println("	background-color: #e2e2e2;");
    	  			out.println("	font-family: Arial, Helvetica, sans-serif;");
    	  			out.println("	font-size: 14px;");
    	  			out.println("}");
    	  			out.println("p {");
    	  			out.println("	margin: 15px;");
    	  			out.println("	font-family: Arial, Helvetica, sans-serif;");
    	  			out.println("	font-size: 12px;");
    	  			out.println("}");
    	  			out.println("#wrapper #bodyArea #footer {");
    	  			out.println("	height: 100px;");
    	  			out.println("	clear: both;");
    	  			out.println("	font-family: Arial, Helvetica, sans-serif;");
    	  			out.println("	font-size: 10px;");
    	  			out.println("	text-align: center;");
    	  			out.println("}");
    	  			out.println("");
    	  			out.println("");
    	  			out.println("#search {");
    	  			out.println("	width: 400px;");
    	  			out.println("	background-color: #DBDBB7;");
    	  			out.println("	float: right;");
    	  			out.println("	height: 30px;");
    	  			out.println("}");
    	  			out.println("");
    	  			out.println("#wrapper #navigation p {");
    	  			out.println("	text-align: left;");
    	  			out.println("}");
    	  			out.println("#wrapper #search p {");
    	  			out.println("	text-align: right;");
    	  			out.println("}");
    	  			out.println("");
    	  			out.println("body {");
    	  			out.println("	background-color: #003;");
    	  			out.println("	background-image: url();");
    	  			out.println("	background-repeat: repeat-x;");
    	  			out.println("}");
    	  			out.println("-->");
    	  			out.println("</style>");
    	  			out.println("<link href='css/layout.css' rel='stylesheet' type='text/css' />");
    	  			out.println("<center>");
    	  			out.println("<div id='wrapper'>");
    	  			out.println("    <div id='navigation'>");
    	  			out.println("      <div class = 'style1'>");
    	  			out.println("	|| <a href='home.html'>Home </a>|| <a href='publish.html'>Publish Note</a> || <a href='profile.html'>Edit Profile</a> || <a href='group.html'>Create Group</a> || Logout ||");
    	  			out.println("	 </div>");
    	  			out.println("    </div>");
    	  			out.println("    <img src='Arterlierlg.jpg' width='800' height='130' alt='artelierlg' />");
    	  			out.println("    <div id='bodyArea'>");
    	  			out.println("      <div id='left'>");
    	  			out.println("        <p>&nbsp;</p>");
    
    
          if (!(username.length() > 0) || !(email.length()  > 0)) {
            request.setAttribute("error",
                "Please fill the forms marked with the asterisk.");
            dispatcher.forward(request, response);
            return;
          }
    
         
    
          Database db = new Database();
         
    
          db.open();
    
          if (db.isUserRegistered(username, email)) {
            request.setAttribute("error", db.getErrorMessages());
            db.close();
            dispatcher.forward(request, response);
            return;
          }
         
          db.registerUser(username, password, email, style, name, surname, age, sex, country, city);
          db.close();
    
    
    	  			out.println("        <p><strong>Registration successful.</strong></p>");
    	  			out.println("      </div>");
    	  			out.println("    <div id='right'>");
    	  			out.println("    </div>");
    	  			out.println("    <div id='footer'>");
    	  			out.println("      <p>&nbsp;</p>");
    	  			out.println("      <p>&copy; 2009 Team 27. All rights reserved.</p>");
    	  			out.println("    </div>");
    	  			out.println("	</div>");
    	  			out.println("</div></center>");
    				out.println("</html>");
    
        } catch (Exception ex) {
          out.println("Exception: " + ex.getMessage());
          out.println("</body>");
          out.println("</html>");
        }
      }
    }// End of class


    Any help?

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Problem:

    "Exception: Cannot forward after response has been committed"

    Explanation:

    From official javax.servlet javadoc:

    "forward should be called BEFORE
    the response ...:

    Java Code:
    ...PrintWriter out = new PrintWriter(response.getWriter(), true);...
    ...out.write( )...
    ...has been committed to the client (before response body output has been flushed).

    If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward."

  3. #3
    nakta is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 13
    Last Post: 09-07-2009, 04:32 PM
  2. I need a back and forward code
    By Jaymz in forum New To Java
    Replies: 9
    Last Post: 10-12-2008, 11:50 PM
  3. Replies: 5
    Last Post: 07-29-2008, 02:13 PM
  4. java.io.Exception: Server returned HTTP response code: 403
    By navishkumarb in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2008, 01:33 PM
  5. jsp:forward action
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •