views:

799

answers:

3
+3  A: 

You need to create a Servlet for that. Create a class which extends HttpServlet and write code in doGet() accordingly that it writes the desired JSON string to the response. You can use Google Gson to convert Java objects to a JSON string.

For example:

// Gather data.
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 111);
map.put("title", "event1");
map.put("start", new SimpleDateFormat("yyyy-MM-10").format(new Date()));
map.put("url", "http://yahoo.com/");

// Convert to JSON string.
String json = new Gson().toJson(map);

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

Then just map this servlet in web.xml on the desired url-pattern.

Instead of a Map you could even create your Javabean class Event:

public class Event {
    private Long id;
    private String title;
    private Date start;
    private URL url;
    // Add/generate getters/setters.
}

You could even use Gson to convert it:

Event event = eventDAO.find(request.getParameter("id"));
String json = new Gson().toJson(event);

This way you can more easy collect them all in a List<Event> which is preferable above a List<Map<String, String>>:

List<Event> events = eventDAO.list();
String json = new Gson().toJson(events);
BalusC
thank you for your solution.i have try code it in jsp and it didn`t work..here is my code:<%@ page import="java.util.HashMap;" %><%@ page import="java.util.Map;" %><%@ page import="com.google.gson.Gson;" %><%Map<String, Object> map = new HashMap<String, Object>();map.put("title", "event1");map.put("start","2009-11-11");// Convert to JSON string.String json = new Gson().toJson(map);// Write JSON string.response.setContentType("application/json");response.setCharacterEncoding("UTF-8");response.getWriter().write(json);%>in event page:events: "TemuDia-1.0/manageapp/getapp.jsp"
I said that you need to create a Servlet for this. I didn't said that you need to do this in a JSP. JSP is not for Java code, it is just receipt for trouble. JSP is a view technology providing a template for HTML/CSS/JS and the capability to use taglibs/EL to control page flow and access data dynamically. What is the problem that you have with servlets? Do you just not understand how to use them or declare them in web.xml? Then just say/ask that so instead of going for the wrong solutions.
BalusC
i have done it in servlet, but it still not appear in jcalendar. the name for this file is app.java..in html $('#calendar').fullCalendar({events: "/app" });
A: 

I've created a servlet and am able to generate JSON script. I'm not sure how to feed the JSON response in fullcalendar. I've tried using the servlet URL. But it didn't help. Kindly help me with a sample code.

Mani
BalusC
A: 

It Work with servlet this is how:

In your servlet put this script

map.put("id", 111); map.put("title", "event1"); map.put("start", new SimpleDateFormat("yyyy-MM-10").format(new Date())); map.put("url", "http://yahoo.com/");

    // Convert to JSON string.
    String json = new Gson().toJson(map);

    ***// Put json between [] to be formatted by Fullcalendar -- Ghazi
    json = "[" + json + "]";***

    // Write JSON string.
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
Ghazi