In the server side code, I am writing a string (which gets written on a browser page) using OutputStreamWriter. This gets written in a new window. I need to be able to write this in the same window.
The class extends HttpServlet and following is the structure of the code:
void foo(HttpServletResponse response...) {
...
OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
response.reset();
response.setContentType("text/html");
out.write("Hello World!"); // Or some html string
out.flush();
out.close();
}
The server side (the servlet) don’t and can’t open a new window (fortunately, otherwise spamming the client with popups would be tremendously easy…). The client (the browser) is the only who can open a new window. Most likely you’ve used one of the following constructs in HTML or JavaScript which will show the result in a new window:
or
or
You need to remove the
target="_blank"to get the response in the current window, or in case you’re using JavaScript, to usewindow.location = 'servleturl';instead.Unrelated to the concrete question, emitting HTML in a servlet is a poor practice. Use JSP instead.
See also: