@porpora

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Javaで日本語(漢字/カタカナ/ひらがな)が?で表示されてしまう

解決したいこと

Javaで迷路のサイトを作ろうとしています。
servletでテキストファイルを読み込み配列化→JSPで表示というところまで作っていたのですが、タイトルや本文に日本語を入れると?で表示されてしまいます。

構成環境

paiza cloud
tomcat9.0.38

該当するソースコード

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

//maze.jspを呼び出すためのサーブレット
public class Start extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            //20X20の配列を作成
            String[][] maze = new String[20][20];
            //テキストファイルを読み込み
            StringBuilder articles = new StringBuilder();
            File file = new File(getServletContext().getRealPath("maze.txt"));
            Scanner scan = new Scanner(file);
            int n = 0;
            //読み込んだ内容を配列に入れていく
            while(scan.hasNext()){
                String line = scan.nextLine();
                articles.append(line).append(System.getProperty("line.separator"));
                maze[n] = line.split(",");
                n++;
            }

            request.setAttribute("maze", maze);
            String view = "/WEB-INF/views/maze.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(view);
        dispatcher.forward(request, response);
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
        <title>迷路</title>
        <link rel="stylesheet" href="/maze2/css/stylesheet.css">
    </head>
    <body>
        <h1>練習</h1>
        <% String[][] maze = (String[][])request.getAttribute("maze"); %>
        <table border = "2" width="10" height="10">
        <% for(String[] M: maze){ %>
        <tr>
        <% for(String m: M){ %>
        <!-- ここからif文 -->
        <% if(m.equals("l")){ %>
            <td><img src="../maze2/picture/load.png"></td>
        <% }else if(m.equals("w")){ %>
            <td><img src="../maze2/picture/wall.png"></td>
        <% }else if(m.equals("g")){ %>
            <td><img src="../maze2/picture/goal.png"></td>
        <% }else if(m.equals("s")){ %>
            <td><img src="../maze2/picture/start.png"></td>
        <% }else{ %>
            <td><img src="../maze2/picture/wall.png"></td>
        <% } %>
        <!-- ここまでif文 --?>
        <% } %><!-- for(内側)の終わり -->
        </tr>
        <% } %><!-- for(外側)の終わり -->
    </table>
    <form action="/move" method="get">
        <button form="up"></button>
    </form>
    </body>
</html>

自分で試したこと

・servletにUTF-8でエンコードするようにコードを書き足す
・tomcatの再起動
・サーバーの再起動
・jspにmetaを追加
以上です。

paiza cloudでは保存するときにどのコードで保存するかは選べません。
もし足りない情報などありましたらすいません。
知恵をお貸しください。

0 likes

No Answers yet.

Your answer might help someone💌