-1

Below is report how would i fix this been i've tried to return the requestdispatcher on the doPost Could someone explain what is going on. I just inserting the parameters in the database what's wrong with my code? thanks

type Exception report


message Cannot forward after response has been committed

description The server encountered an internal error that prevented it from fulfilling this request.

exception 

java.lang.IllegalStateException: Cannot forward after response has been committed
    EventHandler.InsertEvent.doPost(InsertEvent.java:75)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

SERVLET

package EventHandler;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/AddEvent")    

public class InsertEvent extends HttpServlet {
    String title,description, founder, time, date, location, payment;
    int price = 0;
    int bank = 0;
    String query;
    Connection conn;
    Statement stmt;
    ResultSet rs;
    DatabaseConnection dbconn;

    private static final long serialVersionUID = 1L;





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


    }




    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        try{
            title = request.getParameter("title");
            description = request.getParameter("description");
            founder = request.getParameter("founder");
            time = request.getParameter("time");
            date = request.getParameter("date");
            location = request.getParameter("location");
            payment = request.getParameter("payment");
            price = Integer.parseInt(request.getParameter("price"));
            bank = Integer.parseInt(request.getParameter("bank"));
            System.out.println(title);
            System.out.println(description);
            System.out.println(founder);
            System.out.println(time);
            System.out.println(date);
            System.out.println(location);
            System.out.println(payment);
            System.out.println(price);
            System.out.println(bank);
            conn = dbconn.setConnection();
            stmt = conn.createStatement();
            query = "insert into event_list values('"+title+"','"+description+"','"+founder+"','"+time+"','"+date+"','"+location+"','"+payment+"',"+price+","+bank+")";
            int i = stmt.executeUpdate(query);

        }catch(Exception e){
            request.setAttribute("Error", e);
            RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
            rd.forward(request, response);
        }finally{
            RequestDispatcher rd = request.getRequestDispatcher("CreateEvent.jsp");
            rd.forward(request, response);
        }

    }

}

DBCONNECTION

 package EventHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DatabaseConnection {   
    Connection conn;
    Statement stmt;
    ResultSet rs; 
    String url = "jdbc:mysql://localhost:3306/form";  
    String driver = "com.mysql.jdbc.Driver";  
    String userName = "root";  
    String password = "Incorrect00!";  

    public DatabaseConnection(){

    }

    public Connection setConnection(){
        try{
            Class.forName(driver);  
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){

        }
        return conn;
    }

    public ResultSet getResult(String sql, Connection conn){
        this.conn = conn; 
        try{
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
        }catch (Exception e){

        }
        return rs;
    }
}  

JSP

<html>
<head>
<title>Create An event</title>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-datetimepicker.min.js"></script>
<script src="js/CreateEvent.js"></script>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css"/>



</head>
<body>

<form method="post" action="AddEvent" >
<br>

Title: <input type="text" name="title" ><br><br>
Event Founder: <input type="text" name="founder" ><br><br>
Description: <textarea name="description" ></textarea><br><br>

    Time:

                    <input type='text' class="form-control" name="time" />

 Day: 
                    <input type='text' class="form-control" data-date-format="YYYY/MM/DD" name="date"/>

Where:  <input type="text" name="location"> <br>
Include Payment? <br>
<input type="radio" name="payment" id="showprice" value="yes">Yes<br>
<input type="radio" name="payment" value="no">No<br>

<div id="price">
Price: <input type="text" value="0" name="price">
<br>
Bank Account Number : <input type="text" name="bank" value="0">
</div>

<input type="submit" value="Finish" class="btn btn-success"/>
</form>
<a href="/EventPlannerV3">Show List</a>
</body>
</html>
  • Not related to your question, but to the dangerous style that you're using for your code: Read about SQL injection, then use `prepareStatement` and provide individual parameters, rather than concatenating content that you read from a request. Then read about Little Bobby Tables here: http://xkcd.com/327/ – Olaf Kock Oct 03 '14 at 11:50

2 Answers2

3

Problem is you are calling the request dispatcher twice.

}catch(Exception e){
        request.setAttribute("Error", e);
        RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
        rd.forward(request, response);
    }finally{
        RequestDispatcher rd = request.getRequestDispatcher("CreateEvent.jsp");
        rd.forward(request, response);
    }

Your program is catching an exception then it is also executing the finally line you cannot forward the request after you have already forwarded it.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • I'm having a problem inserting it to my database my query is right but it is not getting through my database can you please tell me what's wrong with the code? – Mark Kelvin Mojagan Oct 02 '14 at 18:55
  • What is the original error that your program is catching...try doing catch(Exception e){e.printStackTrace();} it will help to know the error. – brso05 Oct 02 '14 at 18:56
  • Also escape your ' in your query should be \' – brso05 Oct 02 '14 at 18:57
  • You don't even have ' on price and bank. You should take the query and try to execute it in sqlserver or something to see what syntax error you get or print out your query before you execute so you can see it...your query is probably not correct syntax – brso05 Oct 02 '14 at 18:59
  • price and bank is an integer type you don't need to put ' on an integer – Mark Kelvin Mojagan Oct 02 '14 at 19:06
1

Use your .forward() mehtod wisely . Also dont forget to add the return statement in case if you have multiple forwards in the same class. read here and https://stackoverflow.com/questions/19619949/how-does-function-terminate-with-requestdispatcher-forward .

catch(Exception e){
        request.setAttribute("Error", e);
        RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
        rd.forward(request, response);
        return; 
    }finally{
        RequestDispatcher rd = request.getRequestDispatcher("CreateEvent.jsp");
        rd.forward(request, response);
        return;
    }

So that the program control is reset , instead of executing other block. you have used the RequestDispatcher in the finally() so that it will be executed always independent of your errors. try to use finally in a wise manner.

  1. When should I use the finally-block in Java's try-catch-finally

  2. Why do I need to use finally to close resources?

Hope this helps !!

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56