Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT: How do I return plain text from my servlet?

Tags:

gwt

I'm using GWT 2.4. I have a form in which I create a submit button like so

    private Button createSaveButton() { 
            final Button saveButton = new Button("Save"); 
            saveButton.getElement().setAttribute("name", SaveXmlServlet.SAVE_BUTTON_PARAM_NAME); 
            saveButton.addClickHandler(new ClickHandler() { 
                    @Override 
                    public void onClick(ClickEvent event) { 
                            formPanel.submit(); 
                    } 
            }); 
            return saveButton; 
    }       // createSaveButton 

I define an onComplete handler for the form like so

            formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() { 
                    @Override 
                    public void onSubmitComplete(SubmitCompleteEvent event) { 
                            Window.alert(event.getResults()); 
                    } 
            }); 

The servlet I submit the form to returns results as plain text.

            res.setContentType("text/plain"); 
            final PrintWriter out = res.getWriter(); 
            out.print(saveSucceeded); 
            out.close(); 

However, when I actually do the alert, it will attach "<pre>" tags to the output. For example, if the servlet outputs "true", what is alerted to the user is "<pre>true</pre>". How do I get this only to output what was written to the response? I could do some string manipulation to remove the "<pre>" tags, but that seems like I'm not addressing the core issue. Thanks, - Dave

like image 427
Dave Avatar asked Nov 29 '11 21:11

Dave


2 Answers

There is another solution - to set content type to text/html.

like image 183
Martin Ždila Avatar answered Oct 23 '22 09:10

Martin Ždila


I don't think you can do anything about that, but it's easy to work around without weird text manipulations:

public String getPlainTextResult(SubmitCompleteEvent event) {
  Element label = DOM.createLabel();
  label.setInnerHTML( event.getResults() );
  return label.getInnerText();
}
like image 43
Strelok Avatar answered Oct 23 '22 10:10

Strelok