How to write a JSP page using characters from other languages?

Asked

Viewed 148 times

3

I’m writing a JSP page that uses Japanese characters and I’m using the get method, but when I switch to the post method it doesn’t work. I’ve tried to use charset = Shift_jis, but nothing!!

Page JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset = UTF-8">
        <title>本</title>
    </head>
    <body>
        <form action="SubmitNameServlet"  method="get">
            <label for "name">名前</label>
            <input type="text" name="nome" id="nome"/>
            <br/>
            <input type="submit" value="Send Name"/>
       </form>

    </body>
</html>    

Class of the Servlet:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SubmitNameServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("nome");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        out.println("<!DOCTYPE html>");
        out.println("<html><head><title>日本語</title></head>");
        out.println("<body>");
        out.println("<h1>始めまして " + name + " </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}
  • 1

    Edit the question and post what you have already done.

  • The question is much improved.

2 answers

1

Use

out.println(Charset.forName("UTF-8").encode("<html><head><title>日本語</title></head>"));

or create a method:

private boolean postEncodedStr(PrintWriter out, String strMsg) {
    try {
        out.println(Charset.forName("UTF-8").encode(strMsg));
    } catch (Exception e) {
        return false;
    }

    return true;
}

and use in your code as:

public class SubmitNameServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("nome");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        postEncodedStr(out,"<!DOCTYPE html>");
        postEncodedStr(out,"<html><head><title>日本語</title></head>");
        postEncodedStr(out,"<body>");
        postEncodedStr(out,"<h1>始めまして " + name + " </h1>");
        postEncodedStr(out,"</body>");
        postEncodedStr(out,"</html>");
    }
}
  • thanks for answering , but could show in your reply how to make use of both method you indicated ? So it’s ready to be run on the server!

1

In his method doPost you arrow the encoding to UTF-8. ServletResponse::setCharacterEncoding

response.setCharacterEncoding("UTF-8");

The tag <meta http-equiv="Content-Type" content="text/html;charset = UTF-8"> is not used when the page is served by HTTP, it only serves to tell the encoding when you save the page as a file .html or when you access this file over the network as file://

Browser other questions tagged

You are not signed in. Login or sign up in order to post.