wood burning stoves 2.0*
The moose likes Servlets and the fly likes cant forward after response has been committed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "cant forward after response has been committed " Watch "cant forward after response has been committed " New topic
Author

cant forward after response has been committed

neelima mullapudi
Greenhorn

Joined: May 09, 2012
Posts: 10
please help me
this is my jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@page import = "com.valuelabs.vljt2012.DTO.MoviesDTO"%>
<%@page import = "com.valuelabs.vljt2012.DTO.ActorDTO"%>
<%@page import = "com.valuelabs.vljt2012.DTO.ProducerDTO"%>
<%@ page import=" java.sql.PreparedStatement"%>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Connection"%>
<%@page import = "com.valuelabs.vljt2012.db.Connectiondb"%>
<%@page import = "com.valuelabs.vljt2012.util.Constants"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Movies</title>
</head>
<body background ="map.jpg">

<%

final String selection="";

List<MoviesDTO> movies =(List<MoviesDTO>)request.getAttribute("movies");




String mSelect=Constants.mselect;

%>
<form name="form" action=Application?status="<%=selection %>" method="post">
<script type="text/javascript">
function result(){
if (document.form.getElementById("edit").value){
selection ="edit";
}
else if (document.form.getElementById("delete").value){
selection ="delete";
}
else if (document.form.getElementById("submit2").value){
selection ="email";
}

}

if(request.getAttribute(status).equals("suucessful")){
alert("Action succesfully achieved");}
else if(request.getAttribute(status).equals("unsuucessful") ){
alert("Try again");}
else {}
</script>

<p><a href="Movies.jsp">Add New Movie</a>
<a href="actor.jsp">Add New Actor</a>
<a href="Producer.jsp">Add New Producer</a>
</p><br><center>
<TABLE BORDER="1" >
<TR>
<TH> <b>Name</b> </TH>
<TH> <b>Year of Release</b> </TH>
<TH> <b>Comments</b> </TH>

<TH> <b>Edit</b> </TH>
<TH> <b>Delete</b> </TH>
</TR>
<%

for(MoviesDTO mov:movies)
{
System.out.println("movies length ="+movies.size());
System.out.println("movies length ="+mov.getComments());
System.out.println("movies length ="+mov.getName());


%>
<TR> <TD> <%= mov.getName()%> </TD>
<TD> <%= mov.getYrOfRealease()%></TD>
<TD> <%= mov.getComments()%> </TD>
<TD>
<input type="hidden" name="EachMovieId" value="<%=mov.getMoviesId() %>" />
<input type="hidden" name="EachMovieName" value="<%=mov.getName() %>">
<input type="hidden" name="EachMovieYear" value="<%=mov.getYrOfRealease() %>">
<input type="hidden" name="EachMovieComments" value="<%=mov.getComments() %>">

<INPUT TYPE = "radio" name ="result" value="<%= mov.getMoviesId()%>"id = "edit" name ="edit"></TD>
<TD><input type="hidden" name="EachMovieId" value="<%=mov.getMoviesId() %>" />
<input type="hidden" name="EachMovieName" value="<%=mov.getName() %>">
<input type="hidden" name="EachMovieYear" value="<%=mov.getYrOfRealease() %>">
<input type="hidden" name="EachMovieComments" value="<%=mov.getComments() %>">

<input type = "radio" name ="result" value="<%= mov.getMoviesId()%>"id ="delete"name = "delete"></TD>

</TR>

<%
}
%>

</TABLE>

<p>Send Mail<input type= "radio" value = "Send Email" id = "submit2" onclick="result()" name ="Send Mail"/></p>
<input type= "submit" value = "Submit" id = "submit" onclick="result()"/>
<input type= "reset" value = "Reset" id = "reset" /></center>
</form>
</body>
</html>



this is my servlet page

package com.valuelabs.vljt2012.handlers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.valuelabs.vljt2012.DAO.ActorManager;
import com.valuelabs.vljt2012.DAO.MailManager;
import com.valuelabs.vljt2012.DAO.MovieManager;
import com.valuelabs.vljt2012.DAO.ProducerManager;
import com.valuelabs.vljt2012.DTO.ActorDTO;
import com.valuelabs.vljt2012.DTO.MailDTO;
import com.valuelabs.vljt2012.DTO.MoviesDTO;
import com.valuelabs.vljt2012.DTO.ProducerDTO;
import com.valuelabs.vljt2012.db.Connectiondb;

/**
* Servlet implementation class Application
*/
@WebServlet("/Application")
public class Application extends HttpServlet {
PrintWriter printWriter=null;
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
Connectiondb db= new Connectiondb();
db.setConnection();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

String action= request.getParameter("status");
System.out.println("value of status is "+action);
if(action.equals("addMovies"))
{
System.out.println("hiiiihmmmmm");
addMovies(request, response);
}

else if(action.equals("addActor")){
addActor(request,response);
}

else if(action.equals("addProducer")){

addProducer(request,response);
}

else if(action.equals("delete")){
deleteMovie(request,response);
}

else if (action.equals("edit")){
editMovie(request,response);
}
else if (action.equals("email")){
sendEmail(request,response);
}
if((request.getParameter("name")==null)||(request.getParameter("year")==null)||(request.getParameter("email")==null)){
request.setAttribute("error", "error");
request.getRequestDispatcher("/error.jsp").forward(request,response);
}

else{}




}

public <Session> void sendEmail(HttpServletRequest request,
HttpServletResponse response) {
String email = request.getParameter("email");
String gender = request.getParameter("gender");
MailDTO mail = new MailDTO();
MailManager mails= new MailManager();
mail.setEmail(email);
mail.setGender(gender);
boolean result = mails.sendMail(mail);
if(result){
request.setAttribute("status", "successful");
}
else
{ request.setAttribute("status", "unsuccessful");
}
}


// TODO Auto-generated method stub



private void addProducer(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ProducerDTO produce = new ProducerDTO();
ProducerManager producer = new ProducerManager();
produce.setName(request.getParameter("name"));
produce.setName(request.getParameter("gender"));
produce.setName(request.getParameter("comments"));
printWriter=response.getWriter();
response.setContentType("text/html");
printWriter.println("<h2>Actor Added Successful</h2>");
boolean result= producer.addProducer(produce);
if(result){
request.setAttribute("status", "successful");
}
else
{ request.setAttribute("status", "unsuccessful");
}
}

private void addActor(HttpServletRequest request,
HttpServletResponse response) {
ActorDTO actor = new ActorDTO();
ActorManager actors= new ActorManager();
actor.setName(request.getParameter("name"));
actor.setName(request.getParameter("gender"));
actor.setName(request.getParameter("comments"));

boolean result= actors.addActor(actor);
if(result){
request.setAttribute("status", "successful");
}
else
{ request.setAttribute("status", "unsuccessful");
}


}
private void editMovie(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

MoviesDTO movie = new MoviesDTO();
MovieManager movies = new MovieManager();
String movieId=request.getParameter("movieId");
String movieName=request.getParameter("movieName");
String YearOfRelease=request.getParameter("movieYear");
String movieYear=YearOfRelease;
int newMYear=Integer.parseInt(movieYear);

String Comments=request.getParameter("movieComments");
String newMovieId=movieId;
int newMId=Integer.parseInt(newMovieId);
movie.setName(movieName);
movie.setYrOfRealease(newMYear);
//movieNameBean.setMoviePoster(moviePosterFile);
movie.setComments(Comments);
movie.setMoviesId(newMId);
movies.editMovies(movie);
System.out.println("successful");
printWriter=response.getWriter();
int result =movies.editMovies(movie);
System.out.println("Delete successful");
if(result==1)
{
request.setAttribute("status","successful");

}
else
{
request.setAttribute("status","unuccessful");

}
request.getRequestDispatcher("/view.jsp").forward(request,response);


}

private void deleteMovie(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
MoviesDTO movie= new MoviesDTO();
MovieManager movies= new MovieManager();
String movieId=request.getParameter("movieId");
String newMovieId=movieId;
int newMId=Integer.parseInt(newMovieId);
String newProducerId=request.getParameter("movieProducerId");
int newPId=Integer.parseInt(newProducerId);
movie.setProducerId(newPId);
movie.setMoviesId(newMId);

int result =movies.deleteMovies(movie);
System.out.println("Delete successful");
if(result==1)
{
request.setAttribute("status","successful");

}
else
{
request.setAttribute("status","unuccessful");

}
request.getRequestDispatcher("/view.jsp").forward(request,response);

// TODO Auto-generated method stub

}
private void addMovies(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("hii am in add movies");
MoviesDTO movie = new MoviesDTO();
MovieManager moviess = new MovieManager();
movie.setName(request.getParameter("name"));
movie.setYrOfRealease(Integer.parseInt(request.getParameter("year")));
movie.setComments(request.getParameter("comments"));

boolean result=moviess.addMovies(movie);
System.out.println("number of added movies "+ result);

if(result==true)
{
MovieManager manager=new MovieManager();
ArrayList<MoviesDTO> movies= manager.getAllMovies();
System.out.println("list isssssssssssssss"+movies.toString());
request.setAttribute("movies",movies );
request.setAttribute("status","successful");
request.getRequestDispatcher("/view.jsp").forward(request,response);
return;

}
else
{
request.setAttribute("status","unuccessful");
response.sendRedirect("/Movies.jsp");
return;

}


}

}


i have to add movies
this is my movies jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page errorPage="error.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Adding Movies</title>
<script type="text/javascript">
function validateForm(){
if (trim(document.registration.name.value) == ''){
alert('Please Enter Name');
document.regstration.name.focus();
return false;
}else if (trim(document.registration.year.value) == ''){
alert('Please Enter Year Of Release');
document.regstration.year.focus();
return false;
}else {
document.registration.submit();
}
}
</script>
</head>
<body>
<form action="Application?status=addMovies" name="registration" method="post">
<center>
<b>Name of the Movie</b><input type= "text" name="name" id = "name"/><br>
<b>Year of Release </b><select name="year" id = "year">
<option value = "2000" >2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
</select> <br>
<b>Comments :</b><textarea rows="10" cols= 50"" name="comments"/></textarea><br>
<b>Submit :</b><input type="submit" value ="Submit" name = submit" onclick="validateForm()">
</center>
</form>
</body>
</html>

why do i get the error ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41115
    
  45
That's too much unformatted code to try and make sense of, but going by the subject - you can only either send a response from a servlet, or redirect or forward elsewhere, not both.


Ping & DNS - my free Android networking tools app
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 216

checkout your code..
your flow of CONTROL is WRONG that is you are trying to do something more AFTER you have send response to client..
 
Consider Paul's rocket mass heater.
 
subject: cant forward after response has been committed
 
Similar Threads
Sending the results from a Servlet back to a JSP
Is it possible to call a servlet function using JavaScript?
Online Reservation/Ticket Booking System
How to fill select list from database table