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]

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
> 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.