servlet problem

Hi,

Can anyone tell me why these two pieces of code do not talk to each other?

<form method="GET" action="servlet/EventServlet">

<input name="key" type="text" size="25"/>

<input type="submit" name="event:Search" value="Go" /></form>

and ...

import java.io.*;

import java.util.Enumeration;

import javax.servlet.*;

import javax.servlet.http.*;

publicclass EventServletextends HttpServlet

{

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

// determine event

String event ="Unknown";// new String();

Enumeration parameters = request.getParameterNames();

while(parameters.hasMoreElements())

{

String name = (String) parameters.nextElement();

if(name.indexOf("event") != -1)

{

event = name.substring("event:".length());

}

}

// delegate event-response

if (event.equals("Search"))

{

ServletContext context = getServletContext();

RequestDispatcher rd = context.getRequestDispatcher("/EventSearch.jsp");

rd.forward(request, response);

}

//...other events

}

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

doGet(request, response);

}

}

Server throws this:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfillingthis request.

exception

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

EventHandlerServlet.doGet(EventHandlerServlet.java:129)

javax.servlet.http.HttpServlet.service(HttpServlet.java:689)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

Message was edited by:

avdo77

[3378 byte] By [avdo77a] at [2007-11-27 5:45:16]
# 1
Is there more code at the line saying "//...other events"? If yes, see reply 8 in this thread: http://forum.java.sun.com/thread.jspa?threadID=639865&messageID=3805508
quittea at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi,yes there is... Is that a problem?there are other if statements for other events.
avdo77a at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 3
The servlet continues with executing doGet() after the dispatcher forward call if you don't place a return statement after this call, so be careful what the servlet does afterwards. Maybe another condition is met and another forward call is triggered.
quittea at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 4
So I should place return statement after each dispatcher forward call?
avdo77a at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 5
What it's telling you is that you've called forward after something has been written to the Response output, which might be as little as setting a header. I can't see, in the supplied code, where you might have done this.
malcolmmca at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 6
> So I should place return statement after each dispatcher forward call?Short answer: yes.
quittea at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 7

[nobr]I've done that in jsp file which is in between:

<%@page import="java.util.Calendar"%>

<%@page import="java.io.*"%>

<%@page import="java.sql.*"%>

<%@include file="connection.jsp" %>

<%@ page import="java.io.*,java.util.Enumeration" errorPage="/Error.jsp" %>

<%

// get input parameter userName - INPUT ACTION

String searchKey = request.getParameter("single");

if(searchKey.equals(""))

{

out.println("Parameter for search not found! Please enter it!");

return;

}

session.invalidate();

session = request.getSession(true);

int cl_id1 = (int)(Math.random() * 1000000);

int cl_id2 = (int)(Math.random() * 1000000);

int cl_id3 = cl_id1 + cl_id2;

String cl_id4 = "" + cl_id3;

session.setAttribute("client_id", cl_id4);

int o_id1 = (int)(Math.random() * 1000000);

int o_id2 = (int)(Math.random() * 1000000);

int o_id3 = o_id1 + o_id2;

String o_id4 = "" + o_id3;

session.setAttribute("order_id", o_id4);

//Then generate search results page

%>

<% ResultSet rs = null;%>

<img src="arrow.jpg" alt="arrow" width="13" height="13" /><span class="style5"><strong>

Search Results</strong></span>

<table border=1>

<tr><td><b>Title</b></td>

<td><b>Price (AU$)</b></td>

<td><b>Category</b></td>

<td><b>ProdYear</b></td>

<td><b>Classification</b></td>

<td><b>Actors</b></td>

<td><b>Quantity</b></td>

<td><b>Add</b></td></tr>

<%

Connection connection = connectDB();

Statement statement = connection.createStatement();

String queryStatement = "select * from dvd where Kind = 'Movie' and Title like '%" + searchKey + "%'";

rs = statement.executeQuery(queryStatement);

%>

<form action="EventHandlerServlet">

<%while(rs.next())

{

%>

<tr><td><% out.println(rs.getString("Title")); %></td>

<td><% out.println(rs.getString("Price")); %></td>

<td><% out.println(rs.getString("Category")); %></td>

<td><% out.println(rs.getString("ProdYear")); %></td>

<td><% out.println(rs.getString("Classification")); %></td>

<td><% out.println(rs.getString("Actors")); %></td>

<td><input name="quantity" type="text" size="1" /></td>

<input type="hidden" name="ID" value="<% out.println(rs.getString("DVD_ID"));%>">

<td><input type="submit" name="event:AddToCart" value="Add"></td></tr>

<%

} //end while%></form>

</table><br>

<input type="submit" name="event:ViewCart" value="View Shopping Cart!">

<input type="submit" name "event:CheckOut" value="Go to Check Out!">

<input type="submit" name="event:Quit" value="Quit!"> </form> <hr>

Don't pay attention to variables they might be slightly different because this is newer code[/nobr]

avdo77a at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...
# 8
I'll do that thanks.
avdo77a at 2007-7-12 15:26:51 > top of Java-index,Java Essentials,Java Programming...