Problem when passing a parameter to a servlet

Hi there

I treid to pass a variable from a servlet to a JSP page, and meanwhile it returns a value to a j2me program as well. Here's my code:

servlet:

response.setContentType("text/html, charset=UTF");

String input = null;

DataOutputStream dos = new DataOutputStream(response.getOutputStream());

//response

InputStream is = request.getInputStream();

DataInputStream dis = new DataInputStream(is);

try{

input = dis.readUTF();

}catch(EOFException e){

}

if(input.startsWith("Voice")){

request.setAttribute("Mode",input);

dos.writeUTF("1");

}else (input.startsWith("Text")){

request.setAttribute("Mode",input);

dos.writeUTF("2");

}

RequestDispatcher rd = request.getRequestDispatcher("/result.jsp");

rd.forward(request, response);

j2me

hc = (HttpConnection)Connector.open(url,Connector.READ_WRITE);

(blah blah, blah to establish a connection)

//receive from servlet

dis = new DataInputStream(hc.openInputStream());

***return1Str = dis.readUTF(); ***

System.out.println("Receive from servlet " + return1Str);

if(return1Str.equals("2")){

textInterface();

}

if(return1Str.equals("1")){

playSound();

recordSound();

}

jsp

i use request.getAttribute

the *** part is where the error occurs, the error says

:Error is: java.io.EOFException

The program works without trying to pass the parameter to jsp, i.e, doesn't have the line

RequestDispatcher rd = request.getRequestDispatcher("/result.jsp");

rd.forward(request, response);

Looks like the code above erases the data in the outputstream so that the j2me file can't find anything in the dataInputStream.

I don't know what's going on, any help will be very appreciate.

Thank you in advance

Mars

[1926 byte] By [marsparka] at [2007-10-2 21:10:55]
# 1

p.s, in server log, the error says

java.lang.IllegalStateException: getOutputStream() has already been called for this response

in the line where "rd.forward(request, response); " is

i'll keep searching on the net and hope i can understand what's going on

updated:

The error exists because I can't call getOutputStream() twice. Because I don't need efficiency but a working solution, i tried to send the same variable to 2 different servlet, the 2nd servlet tries to use its getOutputstream to write to jsp instead of answering back. The error has been removed, but the question now is that:

The jsp file doesn't show any result. Wondering if i'm doing right or not.

servlet:

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

String input = null;

//DataOutputStream dos = new DataOutputStream(response.getOutputStream());

//response

InputStream is = request.getInputStream();

DataInputStream dis = new DataInputStream(is);

try{

input = dis.readUTF();

}catch(EOFException e){

}

if(input.startsWith("Voice")){

request.setAttribute("Mode",input);

}else if(input.startsWith("Text")){

request.setAttribute("Mode",input);

}else if(input.startsWith("Record")){

}else{

}

RequestDispatcher rd = request.getRequestDispatcher("/result.jsp");

rd.forward(request, response);

}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

doGet(request,response);

}

public void destroy(){}

jsp:

<HTML>

<BODY>

Mode is: <%= request.getAttribute("Mode") %>

</BODY>

</HTML>

Message was edited by:

marspark

Message was edited by:

marspark

marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
bump, still cant fix
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> java.lang.IllegalStateException: getOutputStream() has already been called for this response

> in the line where "rd.forward(request, response); " is

So don't open the outputstream before you call forward. You do that in this line:

DataOutputStream dos = new DataOutputStream(response.getOutputStream());

The code makes no sense. You output data to the browser, but then you ALSO forward to a JSP :s That is not possible. Either send output or redirect/forward, you can't do both.

gimbal2a at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Did you verify what was read in by dis.readUTF()?readUTF is used to read in a string that has been encoded using a modified UTF-8 format.Could dis.readUTF() return some strange characters instead of what was expected?
mmccloha at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks for the reply.

I didn't output data to the browser, i used the 1st dataoutputstream to send data back to the user, which is the j2me program.

But anyway, the dataoutputstream problem has already been fixed, the problem now is i can't get a jsp working(the details are in the update), don't know what's wrong with it.

marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thank you for your reply.The dis.readUTF() return is fine, the problem I'm having now is the result.jsp doesn't show anything after forwarding. Please examine the code if you have some spare time:p
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Try change Mode is: <%= request.getAttribute("Mode") %> to Mode is: <%= (String) request.getAttribute("Mode") %>
mmccloha at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks for the reply, but it still doesn't work. I'm doubting if the parameter is transferred to the result.jsp at all
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Was there any error logged?If no error was logged, then could it be that input did not start with either "Voice" or "Text", therefore, no attribute set?
mmccloha at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

The illegal state exception is because you're writing to the response stream, and then trying to forward.

You can't do both. When you start sending data to the client, you're committed to sending all of the data. So, as soon as you start writing to the OutputStream, your going to get the IllegalStateException when you try to forward.

whartunga at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Thanks for reply, there's no error logged and the set attribute is called as the program went through the if condition.
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Thanks for the reply, the outputstream problem has been fixed, the problem I have now is i can't seem to forward the attributes to the jsp file. The detailed code is in replay 2.
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
bump still can't fix it:(
marsparka at 2007-7-13 23:57:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...