0

I have a table testemployee with three column eid,ename,esalary in database.I want to show all rows of table in a json object(later i want to give the json obj a html view using kendo UI grid) I used arraylist to store all rows of data then converted the list to json.But when i run the file it shows HTTP Status 405 - HTTP method GET is not supported by this URL
Below is my code

  package com.JSONoflist;

  import com.google.gson.Gson;

  import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Statement;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

@WebServlet(urlPatterns = {"/list2json"})

public class list2json extends HttpServlet {

protected void processRequest(HttpServletRequest request,     HttpServletResponse response) throws IOException {
       response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    Statement stmt = null;

     ResultSet rs = null;
    dbconn Obj = new dbconn();
    Connection connection = Obj.Open();
       try {

      String qry = "SELECT * FROM testemployee";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(qry);

      JSONObject jObj = new JSONObject();
         ArrayList<Employee> list = new ArrayList<Employee>();
         Employee sPojo = null;
         while (rs.next()) {
          sPojo = new Employee();
          sPojo.setId(rs.getString("eid"));
         sPojo.setEname(rs.getString("ename"));
          sPojo.setSalary(rs.getString("esalary"));
          list.add(sPojo);
       }
       //  String json = new Gson().toJson(list);
        System.out.println(list);
        jObj.put("testemployee", list);
       System.out.println(jObj.toString());
    } catch (Exception ex) {
      ex.printStackTrace();
   }
}

}

i also tried with static value in Arraylist but http error.Am i missing something? please help me.Because of this error i cannot even check json is showing database values in url or not.

@WebServlet(urlPatterns = {"/list2json"})
public class list2json extends HttpServlet 
{

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

   try {
      List<String> foo = new ArrayList<String>();
      foo.add("A");
       foo.add("B");
      foo.add("C");

      String json = new Gson().toJson(foo);

    } catch (Exception ex) {
      ex.printStackTrace();
   }
}

}

1
  • thank you so much but list is not showing.. Would you help me with that? Commented Jul 16, 2015 at 6:56

5 Answers 5

2

Just rewrite your servlet code to

@WebServlet(urlPatterns = {"/list2json"})
public class list2json extends HttpServlet 
{

   @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

   try {
      List<String> foo = new ArrayList<String>();
      foo.add("A");
       foo.add("B");
      foo.add("C");

      String json = new Gson().toJson(foo);

    } catch (Exception ex) {
      ex.printStackTrace();
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The documentation for HttpServlet makes it clear what functions are to be used when expecting specific HTTP methods.

  • doGet is to be used for GET requests.
  • doPost is to be used for POST requests.
  • doPut is to be used for PUT requests, and
  • doDelete is to be used for DELETE requests.

Your methods need to override one of those in order for it to register a valid HTTP method.

Comments

0

Add this method to your servlet. You need to override the method doGet from HttpServlet in order to handle GET requests.

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
   processRequest(request, response); 
} 

2 Comments

well, you need to send the json formatted as kendo ui grid expects. Check this documentation docs.telerik.com/kendo-ui/web/grid/data-binding, to see how your json should look like.
but json values are not showing in my url.it can not fetch the database data in json.first i have to read data from database
0

Use the following pattern:

ArrayList<String> myList = Array.asList(myArray);
myList.add(foo);
myList.add(bar);
JsonArray jsonArray = new JsonArray(myList);

Comments

0

Try renaming :-

protected void processRequest(HttpServletRequest request, HttpServletResponse response) to

protected void doGet(HttpServletRequest request, HttpServletResponse response)

We need to override doGet()/doPost() for get/post request, else it will call default doGet()/doPost() methods which has the functionality of giving output as :-

HTTP Status 405 - HTTP method GET is not supported by this URL

Find more about Servlets here

For printing list do something like this:-

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            List<String> foo = new ArrayList<String>();
            foo.add("A");
            foo.add("B");
            foo.add("C");

            String json = foo.toString();
            out.print(json);

        } catch (Exception ex) {
            ex.printStackTrace();
            out.print(ex.getMessage());
        }
    }

For printing/output you need to make use of out object's print() like out.print(json)

3 Comments

@Sarah5 try the above edited code. make use of out object
i tried out.print method ..but database value is not showing static values are showing but database value is not showing
@Sarah5 you must be doing something wrong while fetching the DB values. I only told you how to send values to output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.