I am a newbie to mngodb, the question may sound little simple but I have tried many ways possible and ended up in trouble. I have successfully inserted data to my database in mongodb and now I need to retrieve those data. I am getting the response in my jsp and I want to pass the string to a servlet where I am using Gson to parse the content. I am missing something in my codes, can anybody help me out here , I am in real trouble.
thanks in advance
jsp file
<body>
<form name="viewData" action="HelloWorld" method="post">
<%
MongoClient mongoClient = null;
mongoClient = new MongoClient();
DB db = mongoClient.getDB("parkingdata");
DBCollection coll;
coll = db.getCollection("parkingDetails");
BasicDBObject docdetails = new BasicDBObject();
DBCursor cursor = coll.find();
JSON json = new JSON();
Object obj = new Object();
String serialize = json.serialize(cursor);
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
System.out.println("serialized String " + serialize);
request.setAttribute("jsonData", serialize);
} finally {
cursor.close();
}
mongoClient.close();
%>
<input type="submit" name="click" value="Click Me ">
</form>
</body>
servlet class
public class HelloWorld extends HttpServlet {
class Temp
{
public String FirstName;
public String LastName;
public String VehicleType;
public String VehicleModel;
public String VehicleNumber;
public String ContactNumber;
public String Email;
};
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String jsonData =(String) req.getAttribute("jsonData");
System.out.println("Data in servlet" + jsonData);
Gson test = new Gson();
Temp data= test.fromJson(jsonData, Temp.class);
resp.setContentType("text/plain");
if (data != null)
{
resp.getWriter().println(data.FirstName);
resp.getWriter().println(data.LastName);
resp.getWriter().println(data.VehicleType);
resp.getWriter().println(data.VehicleModel);
resp.getWriter().println(data.VehicleNumber);
resp.getWriter().println(data.ContactNumber);
resp.getWriter().println(data.Email);
}
}
}
Aucun commentaire:
Enregistrer un commentaire