2

i'm developping a program in java ee.

i know how to display a view from a servlet with a code like this

this.getServletContext().getRequestDispatcher( VUE).forward( request, response );

where my view (VUE) is in a jsp page.

i know also how to return a json object with something like that

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonErreurEtRole); 

I'm using google Gson to convert java objects.

Now i want to return a json object that contain a view in one field and some others messages in others field.

But i can't find how to do it.

Is there a way for that ?

EDIT

I will try to be more clear,

My problem is that : users ara requesting content by calling a servlet (a controller), i check their right on that content. And if they don't have right i send a message like "you don't have right ..." that message should not replace any content in the html page but just appear as a notification.

So i wanted to return a json object like [message, content]

by the way i solved my problem by sending only message in a json object if user don't have right and html content if he has. And in je jquery code i'm testing the return type of the response.

I used this post :

jQuery ajax returned data: json and html mix?

Community
  • 1
  • 1
alpha041
  • 21
  • 3
  • I think if you have a String field in your object, for example `obj.html = "

    Look at me!

    ";` GSon will be able to serialize it properly. Have you tried that?
    – A4L Sep 30 '14 at 08:08
  • i can do like that but my views is elsewhere in a jsp page and i want to maintain that separation. The views are complex. – alpha041 Sep 30 '14 at 08:11
  • Well I'm not aware of a way to capture the html created from a jsp on server side before sending it to the client. You could use some other templating technics to create your view html. [Velocity](http://velocity.apache.org/) for instance. This way you could create the html of your view and save it to string fields and do whatever you want with it, including writing it to servlet's output stream, i.e. sending it to the client just as a jsp does. – A4L Sep 30 '14 at 08:18
  • what is jsonErreurEtRole ? – Yogesh Prajapati Sep 30 '14 at 08:51
  • 1
    @A4l ok thanks i will look at it. JsonErreurEtRole is a String variable. it contain my json object. – alpha041 Sep 30 '14 at 09:07
  • Not sure what you want to achieve, but I could imagine that the following hack works: Wrap a ServletResponseWrapper around your response and override the servlet output stream with a buffer. Then apply the filter chain, grab the HTML from your buffer, and feed that into your JSON response. It feels there is a better solution though if you let us know what you are trying to achieve. – Simon Fischer Sep 30 '14 at 09:41
  • You could also issue a separate HTTP request (URLConnection or HttpClient) from your server back to itself to render the jsp, and set that response body as your json field. – Tap Sep 30 '14 at 16:01
  • @SimonFischer I'm trying this solution only, do you know any libraries that can help me ebedding the html into Json response I want to send. – Aditya Anand Krishna May 19 '17 at 05:15

0 Answers0