0

i want to print URL into next JSP page while that has hindi language text content as follow :

enter image description here

but URL printed as : https://hi.wikipedia.org/wiki/%E0%A4%B5%E0%A4%BE%E0%A4%B0%E0%A4%BE%E0%A4%A3%E0%A4%B8%E0%A5%80

html code of index.html

<script type="text/javascript">
function GetDynamicTextBox(value){
return '<Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enter the URL : </label>' +
 '<input name = "habits" type="text" class="urls" value = "' + value + '" />' +
        '&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Remove" onclick = "RemoveTextBox(this)" /><br><br>'
}
function AddTextBox() {
var div = document.createElement('DIV');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}

function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}

function RecreateDynamicTextboxes() {
var values = eval('<%=Values%>');
if (values != null) {
    var html = "";
    for (var i = 0; i < values.length; i++) {
        html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
    }
    document.getElementById("TextBoxContainer").innerHTML = html;
 }
 }
 window.onload = RecreateDynamicTextboxes;
 </script>



<html>
<head>
<title>T-SUMM</title>

</head>
<body>

   <center>
   <form method="Post" action="./result.jsp">
   <table>
   <br>   <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>
<Label>     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter the URL :  </label>
  <input name='habits' class="urls" id='t2'>
    &nbsp;&nbsp;<input id="btnAdd" type="button" value="add another URL"       onclick="AddTextBox()" /><br><br>
     <div id="TextBoxContainer">
     <!--Textboxes will be added here -->
     </div>

  <input type="submit" name="submit" > 

 </table>
 </form>

  <br><br> 
  </center>
  </body>
 </html>

JSP page as result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <html>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>T-SUMM</title>
  </head>

  <body>
    <% 
    String[] values=request.getParameterValues("habits");

    for(int i=0;i<(values.length);i++)
    {
    out.println("<br><br>&nbsp;"+values[i]);out.println();
    }
    %>
  </body>

  </html>

2 Answers2

0

You can simply achieve this from JavaScript.

var originalURL = "https://hi.wikipedia.org/wiki/%E0%A4%B5%E0%A4%BE%E0%A4%B0%E0%A4%BE%E0%A4%A3%E0%A4%B8%E0%A5%80";

console.log(decodeURIComponent(originalURL));

If you need to decode the URL using Java then following code will do but it won't work in your case.

try {
  System.out.println(java.net.URLDecoder.decode(URL_TO_DECODE, "UTF-8"));
} catch (Exception e) {
  System.out.println(e);
}

Output i'm getting is https://hi.wikipedia.org/wiki/???????

See this answer.

You are nearly there. EncodeURIComponent correctly encodes to UTF-8, which is what you should always use in a URL today.

The problem is that the submitted query string is getting mutilated on the way into your server-side script, because getParameter() uses ISO-8559-1 instead of UTF-8. This stems from Ancient Times before the web settled on UTF-8 for URI/IRI, but it's rather pathetic that the Servlet spec hasn't been updated to match reality, or at least provide a reliable, supported option for it.

(There is request.setCharacterEncoding in Servlet 2.3, but it doesn't affect query string parsing, and if a single parameter has been read before, possibly by some other framework element, it won't work at all.)

So you need to futz around with container-specific methods to get proper UTF-8, often involving stuff in server.xml. This totally sucks for distributing web apps that should work anywhere. For Tomcat see http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

Update 1: Very simple JavaScript function to do your requirement. No need to involve java.

function decodeURL() {
  document.getElementById("result").innerHTML = decodeURIComponent(document.getElementById("t1").value);
}
<!-- URL Decoder -->
<center>
  Enter a URL : <input name='habits' id='t1'>
  <br><br>
  <input type="button" value="Decode" onclick="decodeURL()" />
  <br><br>
  <div id="result">
    result will show here..
  </div>
</center>

If you want to use above function in complete, get the code below.

<html>

<head>
  <title>T-SUMM</title>
</head>

<body>
  <!-- URL Decoder -->
  <center>
    Enter a URL : <input name='habits' id='t1'>
    <br><br>
    <input type="button" value="Decode" onclick="decodeURL()" />
    <br><br>
    <div id="result">
      result will show here..
    </div>
  </center>

  <script type="text/javascript">
    function decodeURL() {
      document.getElementById("result").innerHTML = decodeURIComponent(document.getElementById("t1").value);
    }
  </script>
</body>

</html>

Update 2

function GetDynamicTextBox(value) {
  return '<Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enter the URL : </label>' +
    '<input name = "habits" type="text" class="urls" value = "' + value + '" />' +
    '&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="Remove" onclick = "RemoveTextBox(this)" /><br><br>'
}

function AddTextBox() {
  var div = document.createElement('DIV');
  div.innerHTML = GetDynamicTextBox("");
  document.getElementById("TextBoxContainer").appendChild(div);
}

function RemoveTextBox(div) {
  document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}

function RecreateDynamicTextboxes() {
  var values = eval('');
  if (values != null) {
    var html = "";
    for (var i = 0; i < values.length; i++) {
      html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
    }
    document.getElementById("TextBoxContainer").innerHTML = html;
  }
}
window.onload = RecreateDynamicTextboxes;

function decodeURL() {
  var textFields = document.getElementsByName('habits');
  document.getElementById("result").innerHTML = "";
  for (var x = 0; x < textFields.length; x++) {
    document.getElementById("result").innerHTML += "<p>" + decodeURIComponent(textFields[x].value) + "</p>";
  }
}
<center>
  <form method="Post" action="./result.jsp">
    <table>
      <br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>
      <Label>     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter the URL :  </label>
      <input name='habits' class="urls" id='t2'> &nbsp;&nbsp;
      <input id="btnAdd" type="button" value="add another URL" onclick="AddTextBox()" /><br><br>
      <div id="TextBoxContainer">
        <!--Textboxes will be added here -->
      </div>

      <input type="button" value="Decode" onclick="decodeURL()" />

    </table>
  </form>

  <br>
  <div id="result">
    result will show here..
  </div>
Community
  • 1
  • 1
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
  • i am new for javacript please write the solution with my html and jsp code – sanjay gupta Mar 21 '17 at 11:13
  • @sanjaygupta please reference the **Update 1** on my answer – CodeMonkey Mar 21 '17 at 12:05
  • see my result.jsp page, in this code button event is not given, there is more than one URL are present into "values[]" variable , forgot about index.html page , please resolve result.jsp page without button event fire, then how to decode exact multiple URL(s) present in "values[]" variable ? – sanjay gupta Mar 21 '17 at 14:31
  • On your original form there is 1 textfield to capture the URL to decode. If you have multiple URLs to decode then add a textfield dynamically. If i'm not mistaken even you're assigning the `habits` values to an array from server side there is only 1 argument coming through the form. – CodeMonkey Mar 21 '17 at 14:35
  • The reason not to use a form is it doesn't do anything here. Just a simple input element(s) suffice the requirement. This is the beauty of using JavaScript. – CodeMonkey Mar 21 '17 at 14:37
  • first this code 'String[] values=request.getParameterValues("habits");' will run then after decode the URL(s) and write into result.jsp page – sanjay gupta Mar 21 '17 at 14:41
  • This `String[] values=request.getParameterValues("habits");` will only execute when the `result.jsp` page is called upon right? So which page is you use first? this or `index.html`? – CodeMonkey Mar 21 '17 at 14:47
  • first page is index.html then after result.jsp , i edited the code index.html , now multple URL can be enter by using dynamic text box – sanjay gupta Mar 21 '17 at 15:10
  • please give solution for multiple URL(s) can be write on result.jsp page – sanjay gupta Mar 21 '17 at 15:12
  • thank you so much , you are written value into same page but i want to write the values in next result.jsp page , please tell me how i can do ? – sanjay gupta Mar 27 '17 at 05:16
  • @sanjaygupta if you're happy with browser cookies then i can give you a solution. If not then Javascript is not the answer you're looking for. – CodeMonkey Mar 27 '17 at 05:26
0

result.jsp page can be edited as follow :

  <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>T-SUMM</title>
</head>
<body>
<%@ page import="java.net.URLDecoder"%>
<% request.setCharacterEncoding("UTF-8");
String[] values=request.getParameterValues("habits");
for(int i=0;i<(values.length);i++)
 {  
    String decoded = URLDecoder.decode(values[i]);
    out.println("<br><br>&nbsp;"+decoded);out.println();
 }
  %>
</body>
</html>