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

  • SEARCH
  • Home
  • 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 7796673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:26:26+00:00 2026-06-01T23:26:26+00:00

I am using this servlet to extract the HTML contents from another domain to

  • 0

I am using this servlet to extract the HTML contents from another domain to include in my own page with Ajax, it specifies the response as “UTF-8”:

public class ProxyServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException  {
        String urlString = request.getParameter("url");
        try {
            URL url = new URL(urlString);
            url.openConnection();            
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
            char[] buf = new char[4 * 1024];
            int len;
            while ((len = reader.read(buf, 0, buf.length)) != -1) {
              out.write(buf, 0, len);
            }
            out.flush();
        }
        catch (MalformedURLException e) {     
            throw new ServletException(e);
        }
        catch (IOException e) {     
            throw new ServletException(e);
        }
    }
}

The document I am extracting has a meta tag like this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>

I copied and pasted it onto my own page so it matches exactly. According to the browser page info it is definitely using “UTF-8” encoding. Yet I am still getting “” instead of “&nbsp” in the extracted html contents.

They are actually contained in the responseText from this ProxyServlet. I thought explicitly defining the response content type and output stream charset would handle this but I must be missing something? Has anyone resolved this before.

  • 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-01T23:26:27+00:00Added an answer on June 1, 2026 at 11:26 pm

    Instead of converting a byte stream to chars and vice versa you could just copy from ony bytes stream to another via a byte[] buffer. Or use a Spring util:

    FileCopyUtils.copy(uri.getInputStream(), response.getOutputStream());
    

    or explicitly:

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
    out.flush();
    

    It would guarantee that data is copied as is (without possible screwing things up via wrong chars)

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

Sidebar

Related Questions

I am passing data from Swing to Servlet using this code: URL url =
How can I upload files to server using JSP/Servlet? I tried this: <form action="upload"
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
Using this sample : I have make my own Fragment that holds tabhost and
I'm using Tomcat 6.0.33 with Java 6. I have this servlet ... public class
I have this servlet which needs to send mail using Java Mail API, however
I am currently using this WebDAV Java Servlet Implementation, it's as far as I
I am using this link to study Spring MVC. As described dispatcher-servlet is at
I am using a servlet that is taking parameters from a registration form, what
Using this file as source, I have a situation where I need to retrieve

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.