Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Servlet Response adding JSP page code to it

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all,
I have a servlet where i am forming this xml string and passing it back to the JSP.
The servlet code:

package wwxchange;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


import wwxchange.utility.*;
import wwxchange.beans.*;


public class AutoComplete extends HttpServlet {

private ServletContext context;

//Initialize global variables
public void init(ServletConfig config) throws ServletException {
this.context = config.getServletContext();
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String searchBy = request.getParameter("searchBy");
String id = request.getParameter("id");
String action = request.getParameter("action");
boolean added = false;
ArrayList list = new ArrayList();
StringBuffer sb = new StringBuffer();

HttpSession session = request.getSession();
UserAcctBean user = (UserAcctBean) session.getAttribute("currentuser");
AirbillBean ab = (AirbillBean) session.getAttribute("currentshipment");
String loginID = user.getLoginID();
String accountNbr = user.getAcctNum();

if(id != null)
id = id.trim().toLowerCase();

list = ab.getNamesForAutoComplete(id, accountNbr, searchBy);

for(int i=0;i<list.size();i++) {
sb.append("<customer>");
sb.append("<companyName>" + list.get(i) + "</companyName>");
sb.append("</customer>");
added = true;
}
if(added) {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<search>" + sb.toString() + "</search>");
} else {
//nothing to show
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}

context.getRequestDispatcher("/CreateShipment.jsp").forward(request, response);


}

//Clean up resources
public void destroy() {
}
}


When I access this response from the JSP, it is adding the JSP code from the CreateShipment.jsp page to ehich i am forwarding the respose to in the response body along with the xml string.

I cant commit the response in the servlet coz i am writing to the writer. That gives me an IllegalStateException. What is it that i am missing here?

Thanks in advance.
harmeet
[ January 07, 2007: Message edited by: Bear Bibeault ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi, I don't think there is anything wrong with your code

Since you are writing an xml response using the writer, you can commit it using:
response.getWriter().flush();
before you forward to other resources.

Hope that helps.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please continue the discussion of this issue in its original location.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    Bookmark Topic Watch Topic
  • New Topic