Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 966829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:13:16+00:00 2026-05-16T02:13:16+00:00

A while back I was given the answer of using JSON to pass things

  • 0

A while back I was given the answer of using JSON to pass things from my application to JavaScript. What I don’t understand is how I actually pass the object to JavaScript I see that you have to use a .json file.

And then what? I am able to convert my Java objects to JSON objects but it’s passing that I can’t get my head around. I am using JSP and Servlets to write my application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:13:17+00:00Added an answer on May 16, 2026 at 2:13 am

    The file extension .json is not so relevant. It’s just all about the HTTP Content Type header. As long as it’s application/json, the webbrowser knows perfectly what to do with it. In terms of JSP/Servlet you basically need to create a servlet class which listens on a certain url-pattern, processes the request parameters or pathinfo accordingly and writes a JSON string to the response in the doGet() method.

    Here’s a fictive example of a servlet which returns city option value/label pairs based on the selected value of a country dropdown, it uses Google Gson to convert fullworthy Java objects to JSON in an oneliner:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String country = request.getParameter("country");
        Map<String, String> cities = cityDAO.find(country);
        String json = new Gson().toJson(cities);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }
    

    Map such a servlet on an url-pattern of /cities in web.xml (or if you want a bit more SEO friendly approach, map it on /cities/* and use request.getPathInfo() instead).

    Finally, in the JS code which is executed during onchange of a country dropdown, you just let it fire an asynchronous (ajaxical) request on this servlet with the selected country as parameter (or pathinfo). I’ll give a jQuery based example since it insanely eases firing ajax requests and traversing/manipulating HTML DOM (the "raw" JS code would otherwise have been up to 5 times as long).

    $(document).ready(function() {
        $('#country').change(function() {
            var selectedCountry = $(this).val();
            var servletUrl = 'cities?country=' + selectedCountry;
    
            $.getJSON(servletUrl, function(options) {
                var city = $('#city');
                $('>option', city).remove(); // Clean old options first.
                if (options) {
                    $.each(options, function(value, label) {
                        dropdown2.append($('<option/>').val(value).text(label));
                    });
                } else {
                    dropdown2.append($('<option/>').text("Please select country"));
                }
            });
        });
    });
    

    Here is the appropriate HTML example of the JSP:

    <select id="country" name="country">
        <c:forEach items="${countries}" var="country">
            <option value="${country.key}">${country.value}</option>
        </c:forEach>
    </select>
    <select id="city" name="city">
        <option>Please select country</option>
    </select>
    

    (assuming that ${countries} is an Map<String, String> in application scope)

    Hope this clears the one and other 🙂

    See also:

    • Related questions with tags [json]+[jsp].
    • Related questions with tags [json]+[servlets].
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A while back, I remember using a piece of code (4-5 lines), added to
A while back I created a lightbox plugin using jQuery that would load a
A while back I was playing with methods using variable-length argument lists (java) that
I've searched and searched and can't find an answer. Actually all the things I've
I was given this code a while back. I finally got around to testing
A while back i read somewhere about how to improve upon the MVC pattern
A while back a found a great-looking framework that allowed .net developers to implement
A while back, smart clients had been touted as the solution to occasional connected
A while back I wrote a simple python program to brute-force the single solution
a while back, CNET had these really cool Social Share buttons, that when you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.