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

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

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


    Java Code:
        "poll": {
            "title": "About your preferences",
            "questions": [
                "Which color do you like best?",
                "What is your lucky number?",
    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".

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

    Java Code:
            <title>JSP Page</title>
                <a><% out.print(request.getAttribute("oldTitle"));%></a><br>
                <input type="text" name="firstname" value="">
                What is your lucky number? <br>
                <input type="text" name="lastname" value="">
                Why? <br>
                <input type="text" name="lastname" value="">
                <input type="submit" value="Done">
    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
    Rep Power

    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, 02:14 AM
  2. Replies: 3
    Last Post: 09-13-2013, 10:05 AM
  3. Replies: 3
    Last Post: 06-20-2012, 08:04 AM
  4. JSON from javascript to java servlet
    By boss-tech in forum Java Servlet
    Replies: 1
    Last Post: 09-18-2009, 09:27 AM
  5. Hash table with separate chaining
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09: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