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 9121337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:50:22+00:00 2026-06-17T05:50:22+00:00

I’m trying to send some simple string to test data transfer between my servlet

  • 0

I’m trying to send some simple string to test data transfer between my servlet and an applet
(servlet -> applet, not applet -> servlet), using Json format and Gson library.
The resulting string in the applet should be exactly the same as the original message, but it isn’t.
I’m getting 9-character < !DOCTYPE string instead.

edit: It looks like the servlet returned HTML web page instead of JSON, didn’t it?
edit2: The message is correctly displayed in a browser, when lauching the servlet using “Run File” command in NetBeans.

Could you please take a look at my code:

Servlet:

//begin of the servlet code extract
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
{
    PrintWriter out = response.getWriter();
    try
    {
        String action;
        action = request.getParameter("action");
        if ("Transfer".equals(action))
        {
            sendItToApplet(response);
        }
    }
    finally
    {
        out.close();
    }
}

public void sendItToApplet(HttpServletResponse response) throws IOException
{
    String messageToApplet = new String("my message from the servlet to the applet");
    String json = new Gson().toJson(messageToApplet);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    Writer writer = null;
    writer = response.getWriter();
    writer.write(json);
    writer.close();
}
//end of the servlet code extract

Applet:

//begin of the applet code extract
public void getItFromServlet() throws MalformedURLException, IOException, ClassNotFoundException
{
    URL urlServlet = new URL("http://localhost:8080/Srvlt?action=Transfer");
    URLConnection connection = urlServlet.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/json");

    InputStream inputStream = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    JsonReader jr = new JsonReader(br);
    String retrievedString = new Gson().fromJson(jr, String.class);
    inputStream.close();
    jtextarea1.setText(retrievedString); //jtextarea is to display the received string from the servlet in the applet
}
//end of the applet code extract
  • 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-06-17T05:50:23+00:00Added an answer on June 17, 2026 at 5:50 am

    The problem is that you’re not sending JSON from your servlet, as you have figured out from your comments. And this is because … Gson is very confused as to what you’re trying to do.

    If you test your JSON serialization (the output from toJson()) in your servlet, you’ll find that … it’s not doing anything and simply returning the contents of your String with quotes around it. JSON is based around a textual representation of an object (the class’ fields to values), and it certainly doesn’t want to do that with a String object; the default serialization there is to get the contents of the String to be put into the resulting JSON.

    Edit to add: A typical use for Gson would be something like:

    class MyClass {
        String message = "This is my message";
    }
    

    …

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

    The resulting JSON would be:

    {“message”:”This is my message”}

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jsonparser to parse data and images obtained from json response. When
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using JSon response to parse title,date content and thumbnail images and place
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is

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.