[英]How to print Hindi text into web page by giving servlet , java and html code
i am using servlet, java and one html code to extract hindi text from following URL : 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 我正在使用servlet,java和一个html代码从以下URL中提取印地文文本: 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
i want to display hindi font by servlet code , code is given as : 我想通过servlet代码显示印地文字体,代码为:
//Extraction1.java //java file
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class Extraction1 {
public String toHtmlString(String url) throws IOException
{
StringBuilder sb = new StringBuilder();
for(Scanner sc = new Scanner(new URL(url).openStream()); sc.hasNext(); )
sb.append(sc.nextLine()).append('\n');
return sb.toString();
}
}
MultiParamServlet3.java // servlet file MultiParamServlet3.java // Servlet文件
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MultiParamServlet3 extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
resp.setContentType("text/html");
String[] values=req.getParameterValues("habits");
Extraction1 t=new Extraction1();
String s=t.toHtmlString(values[0]).replaceAll("\\<.*?>","");
pw.println("<html><head><meta charset=\"utf-8\"></head><body>"+s+"</body></html>");
pw.close();
}
}
index.html // html file index.html // html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form method="Post" action="MultiParamServlet3">
<Label> <br><br> &n bsp; Enter the URL : </label>
<input name='habits' id='t2'>
<input type="submit" name="submit">
</form>
</body>
</html>
servlet program able to print english text after extraction, but hindi text converted as ????? servlet程序能够在提取后打印英文文本,但将印地文文本转换为????? (question mark). (问号)。
how to print hindi text into web page by servlet program ? 如何通过servlet程序将印地文打印到网页中?
You have to set encoding response. 您必须设置编码响应。
change "text/html"
to "UTF-8"
. 将"text/html"
更改为"UTF-8"
。
resp.setCharacterEncoding("UTF-8");
Use StringEscapeUtils class from apache.commons.lang and implement it like: 使用apache.commons.lang中的StringEscapeUtils类,并像下面这样实现:
String output=StringEscapeUtils.unescapeHtml3(responseMessageString);
mathod depends on api version i am using commons.lang 3.3. mathod取决于我使用commons.lang 3.3的api版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.