Issue
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
i want to display hindi font by servlet code , code is given as :
//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
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
<!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 ????? (question mark).
how to print hindi text into web page by servlet program ?
Solution
You have to set encoding response.
change "text/html"
to "UTF-8"
.
resp.setCharacterEncoding("UTF-8");
Answered By - KhAn SaAb