Hi everyone,
I am doing a project for school and i'm having some problems.
I have a Profile page.
When i call on Profile.java, this servlets get(doGet) the profile info from the database and shows it on the profile.jsp.
The user has the option to edit the info on the profile.jsp.
When i post the info, it posts it to the Profile.java (the doPost method).
If everything is updated ok, i want to call on the Profile.java page again. To show the profile info. When i do that i get an error: Cannot forward after response has been committed, and on the page the error shows that the page has been requested more than 20 times.
The code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("binnen get profile2");
//instatiate the connection
DbConnection config = new DbConnection();
//create variables
String stringQuery = "";
ArrayList richtingList = new ArrayList();
HttpSession session = request.getSession(true);
String userId = session.getAttribute("userId").toString();
System.out.println("user_id = "+ userId);
Statement st = null;
ResultSet rs = null;
Connection conn = null;
try{
Class.forName(config.getDriver()).newInstance();
conn = DriverManager.getConnection(config.getUrl() + config.getDbName(), config.getUserName(), config.getPassword());
stringQuery = "select user.username, sha(user.password) as password, user.naam, user.achternaam, user.email, user.geboortedatum, user.registratienr, user_richting.*, richting.naam from user, user_richting, richting where user_richting.user_id = user.id and user_richting.richting_id=richting.id and user.id ="+userId;
System.out.println(stringQuery);
st = conn.createStatement();
rs = st.executeQuery(stringQuery);
while(rs.next()){
//get info
}
}catch(Exception e){
e.printStackTrace();
}
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
System.out.println("binnen post profile");
System.out.println("binnen post profile2");
//System.out.println("email + geboortedatum =" + request.getParameter("email")+request.getParameter("geboortedatum"));
//instantiate the configuration class
DbConnection config = new DbConnection();
Connection conn = null;
HttpSession session = request.getSession(true);
//get post parameters && create variables
String userId = request.getParameter("userId");
String email = request.getParameter("email");
String birthdate = request.getParameter("geboortedatum");
String username = request.getParameter("username");
String password = request.getParameter("password_new");
String strQuery = "";
String goTo = "";
PreparedStatement ps = null;
try {
Class.forName(config.getDriver()).newInstance();
conn = DriverManager.getConnection(config.getUrl() + config.getDbName(), config.getUserName(), config.getPassword());
if (email != null && email != "" && birthdate != null && birthdate != "") {
// update everything and logout
System.out.println(strQuery);
ps = conn.prepareStatement(strQuery);
int i = ps.executeUpdate();
if(i!=0){
System.out.println("update success");
request.setAttribute("message", "update success");
response.sendRedirect("Profile");
}else{
System.out.println("update mislukt");
request.setAttribute("message", "update mislukt");
}
}else{
response.sendRedirect("error.jsp");
//System.out.println("FOut opgetreden");
}
//close connection
conn.close();
//System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
RequestDispatcher dispatcher = request.getRequestDispatcher("Profile");
if (dispatcher != null){
dispatcher.forward(request, response);
}
Help needed please.
Thanx in advance.