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

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 does not work. I already tried using charset = Shift_JIS, but nothing !!

JSP page:

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

Servlet class:

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>");
    }
}
    
asked by anonymous 18.09.2015 / 15:22

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>");
    }
}
    
09.10.2015 / 16:14
1

In your doPost method, you set the encoding to UTF-8 . ServletResponse :: setCharacterEncoding

response.setCharacterEncoding("UTF-8");

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

    
09.10.2015 / 17:08