Home » Java » java – Show URL encoded string as html-Exceptionshub

java – Show URL encoded string as html-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the following code

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
       String baseURL = request.getRequestURL().toString();
       String link1 = "<a href=\"" + baseURL + "firstlink?debug\">First Link</a>";
       String link2 = "<a href=\"" + baseURL + "secondlink?debug\">Second Link</a>";
       String link3 = "<a href=\"" + baseURL + "thirdlink?debug\">Third Link</a>";

       response.setContentType("text/html");
       PrintWriter out = response.getWriter();
       String message = "<HTML>";
       message += "<BR><BR>" + link1 + "<BR><BR>";
       message += link2 + "<BR><BR>";
       message += link3 + "<BR><BR>";
       message += "</HTML>";

       String result = URLEncoder.encode(message, StandardCharsets.UTF_8.name());
       out.println(result);
    }

To print to a webpage.

Instead of the three links I am getting an encoded string like

%3CHTML%3E%3CBR%3E%3CBR%3E%3Ca+href%3D%22http%3A%2F%2 …

How can I display the links? I need to have the URL encoding to satisfy Veracode requirements.

How to&Answers: