Results 1 to 2 of 2
  1. #1
    zumzum is offline Member
    Join Date
    Apr 2017
    Posts
    2
    Rep Power
    0

    Default accessing JSON table from servlet, display content in separate JSP page

    Hi, I have a json file containing some data like so:

    poll.json

    Java Code:
    {
        "poll": {
            "title": "About your preferences",
            "questions": [
                "Which color do you like best?",
                "What is your lucky number?",
                "Why?"
            ]
        }
    }
    In my servlet I read the json file and process the data and store the "title" item from the json table into titleString. after that (here where the trouble starts) I replace the "oldTitle" from my jsp file with "titleString".

    PollServlet.java

    Java Code:
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
            response.setContentType("text/json");
            /* create bufferedReader to read the file.json */
            BufferedReader br = new BufferedReader( new InputStreamReader(
                    getClass().getResourceAsStream("poll/poll.json"))); 
            JsonReader reader = Json.createReader(br);
            br.close();
            /* create json object and store it into titleString */
            JsonObject jsonObject = reader.readObject();
            String titleString = jsonObject
                    .getJsonArray("poll").getJsonObject(0).toString();
             /* replace the item with atribute "oldTitle" to titleString*/
             request.setAttribute("oldTitle",titleString);
             getServletConfig().getServletContext().getRequestDispatcher(
             "/poll.jsp").forward(request, response);
    
        }
    and here is the poll.jsp file where I set the attribute

    Java Code:
            <title>JSP Page</title>
        </head>
        <body>
    
            <form>
                <a><% out.print(request.getAttribute("oldTitle"));%></a><br>
                <input type="text" name="firstname" value="">
                <br>
    
                What is your lucky number? <br>
                <input type="text" name="lastname" value="">
                <br>
                Why? <br>
                <input type="text" name="lastname" value="">
                <br><br>
                <input type="submit" value="Done">
            </form>
    
        </body>
    </html>
    The problem is when I run my project all I get is "null" instead of the titleString in the tag inside the what causes the problem? I think the problem is not accessing and processing the json file correctly

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: accessing JSON table from servlet, display content in separate JSP page

    Are you sure you're supposed to close the BufferedReader?
    Then again, I would have expected that to result in an exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Jackson Json array accessing problem
    By arrow in forum Advanced Java
    Replies: 9
    Last Post: 12-17-2016, 01:14 AM
  2. Replies: 3
    Last Post: 09-13-2013, 09:05 AM
  3. Replies: 3
    Last Post: 06-20-2012, 07:04 AM
  4. JSON from javascript to java servlet
    By boss-tech in forum Java Servlet
    Replies: 1
    Last Post: 09-18-2009, 08:27 AM
  5. Hash table with separate chaining
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •