Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON deserialization failure? (servlet->applet communication)
    primarykey
    data
    text
    <p>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 <strong>&lt; !DOCTYPE</strong> string instead.</p> <p>edit: It looks like the servlet returned HTML web page instead of JSON, didn't it?<br /> edit2: The message is correctly displayed in a browser, when lauching the servlet using "Run File" command in NetBeans.</p> <p>Could you please take a look at my code:</p> <p>Servlet:</p> <pre><code>//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 </code></pre> <p>Applet:</p> <pre><code>//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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload