Bookmark Topic Watch Topic
  • New Topic

Servlet Response adding JSP page code to it

harmeet bawa
Posts: 22
  • Mark post as helpful
  • send pies
  • 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.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("<companyName>" + list.get(i) + "</companyName>");
added = true;
if(added) {
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<search>" + sb.toString() + "</search>");
} else {
//nothing to show

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.
[ January 07, 2007: Message edited by: Bear Bibeault ]
Shu Gao
Posts: 18
  • Mark post as helpful
  • send pies
  • 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:
before you forward to other resources.

Hope that helps.
Bear Bibeault
Author and ninkuma
Posts: 65274
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please continue the discussion of this issue in its original location.
    Bookmark Topic Watch Topic
  • New Topic