0

How can I set the content the as applicaiton/json in jsp ?

I have tried the following code,

<%@ page language="java" contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>

But when I run the JSP file ,I got this and the page doesn't opened in browser.enter image description here

Hope our stack users will give best solution.

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • What are you trying to achieve by setting the JSP response as applicaiton/json? – Luiggi Mendoza Jul 05 '13 at 15:16
  • @ Luiggi Mendoza Please see my previous question http://stackoverflow.com/questions/17421645/spring-jsp-jquery-ajax-json-setting-environment-for-utf-8-issue/17484984?noredirect=1#comment25419824_17484984 . My JSON String from controller returns **????** in javascript alert. – Human Being Jul 05 '13 at 15:21
  • then your problem is from server side, not from client side. If you really need an answer for your old question, just wait for it or wait until the bounty finishes and put a bigger bounty. By the way, in which browsers have you tested this? – Luiggi Mendoza Jul 05 '13 at 15:22
  • In chrome and the IE8 – Human Being Jul 05 '13 at 15:23
  • @ Luiggi Mendoza I can't analyse where is my problem is on **Server side** or **Client side** . – Human Being Jul 05 '13 at 15:30
  • You can do it by inspecting the response using a tool like FireBug for FireFox or Chrome Console by pressing F12 (note that FireBug must be installed prior to pressing F12). – Luiggi Mendoza Jul 05 '13 at 15:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32964/discussion-between-kite-player-and-luiggi-mendoza) – Human Being Jul 05 '13 at 15:36

2 Answers2

0

With the content type set to application/json. JSON string will be downloaded similar to a file since it is not a HTML doc. You need to set the content as text/html. But it is incorrect to return JSON data as content type text/html. What do you want let us know specifically ?

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • The how can I set the response content type as applicaiton/json ? – Human Being Jul 05 '13 at 15:14
  • Do you want to display the JSON string or want to retrieve the data from the JSOn string and perform some other operation ? – AllTooSir Jul 05 '13 at 15:15
  • Please see my previous question http://stackoverflow.com/questions/17421645/spring-jsp-jquery-ajax-json-setting-environment-for-utf-8-issue/17484984?noredirect=1#comment25419824_17484984. My JSON String from controller returns **????** in javascript alert. – Human Being Jul 05 '13 at 15:16
-1

Page direct should work. You can also try

<%
    response.setContentType("application/json");
%>
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • -1: Please refer to [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) before posting such kind of answers. – Luiggi Mendoza Jul 05 '13 at 15:14
  • @Luiggi Thats why i said in the beginning Page directive should work. Second solution is just an alternatve. – M Sach Jul 05 '13 at 15:16
  • First sentence should be a comment. Second option is **highly discouraged** and should not be used as answer. Refer to the link I've posted in my comment. – Luiggi Mendoza Jul 05 '13 at 15:19