如何在URL有hindi文本时将URL打印到jsp页面

时间:2017-03-21 04:39:50

标签: html jsp

我想将URL打印到下一个JSP页面,而其中有印地文语言文本内容如下:

enter image description here

但网址打印为: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

index.html的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页面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 个答案:

答案 0 :(得分:0)

您可以通过JavaScript实现此目的。

&#13;
&#13;
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));
&#13;
&#13;
&#13;

如果你需要使用Java解码URL,那么下面的代码就可以了,但是在你的情况下它不会起作用。

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

输出我得到的是https://hi.wikipedia.org/wiki/???????

请参阅此answer

  

你快到了。 EncodeURIComponent正确编码为UTF-8,   这就是你今天应该在URL中使用的内容。

     

问题是提交的查询字符串被删除了   进入服务器端脚本的方式,因为getParameter()使用   ISO-8559-1而不是UTF-8。这源于古代时代之前   网站以UTF-8为基础确定URI / IRI,但它相当可怜   Servlet规范尚未更新以匹配现实,或者至少提供   它是一个可靠的,受支持的选项。

     

(Servlet 2.3中有request.setCharacterEncoding,但它没有&#t; t   影响查询字符串解析,以及是否已读取单个参数   之前,可能还有一些其他的框架元素,它不会工作   所有。)

     

因此,您需要使用特定于容器的方法来获取   正确的UTF-8,通常涉及server.xml中的内容。这完全很糟糕   用于分发应在任何地方工作的Web应用程序对于Tomcat看   http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

更新1:非常简单的JavaScript函数可满足您的要求。无需涉及java。

&#13;
&#13;
function decodeURL() {
  document.getElementById("result").innerHTML = decodeURIComponent(document.getElementById("t1").value);
}
&#13;
<!-- 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>
&#13;
&#13;
&#13;

如果您想完整使用上述功能,请获取以下代码。

<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>

更新2

&#13;
&#13;
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>";
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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>
<%@ 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>