質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

解決済

2回答

614閲覧

文字化けが解消されない!!!

退会済みユーザー

退会済みユーザー

総合スコア0

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

0クリップ

投稿2020/09/22 01:55

編集2020/09/22 01:58

下記の通りにサーバーで実行したら文字化けが発生してしまいます。どなたか改善策があれば知恵をお貸しいただきたいです。

■Servlet
package seisan;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import ryoukin.java.ryoukin_beans;

/**

  • Servlet implementation class SeisanServlet

*/
@WebServlet("/Seisan")
public class SeisanServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

// Define datasource/connection pool for Resource Injection @Resource(name="jdbc/WebApp") private DataSource dataSource; /** * @see HttpServlet#HttpServlet() */ public SeisanServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @SuppressWarnings("unused") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); //文字化け対策 response.setContentType("text/html; charset=UTF-8"); response.setContentType("text/html; charset=UTF-8"); // 【料金情報を取得する】 ryoukin_beans ryoukin = null; // ■DB接続 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; // 接続情報 String url="jdbc:mariadb://localhost:3306/webApp"; String user="user02"; String password="user02"; try { // 接続処理 conn = DriverManager.getConnection(url, user, password); // ■検索処理実行 // SQL作成 String sql = "SELECT coursea_value, courseb_value, coursec_value,opcoursea_value FROM seisan"; System.out.println(sql); // SQL準備 ps = conn.prepareStatement(sql); // SQL実行 rs = ps.executeQuery(); // データを読込み、JavaBeansに設定する ryoukin = new ryoukin_beans(0, 0, 0, 0, 0, 0); while(rs.next()) { // データ読込 int coursea = rs.getInt("coursea_value"); int courseb = rs.getInt("courseb_value"); int coursec = rs.getInt("coursec_value"); int opcoursea= rs.getInt("opcoursea_value"); // JavaBeansにデータを設定 ryoukin.set(coursea); ryoukin.set(courseb); ryoukin.set(coursec); ryoukin.set(opcoursea); } System.out.println(ryoukin); } catch(SQLException e) { // 接続失敗時の処理 e.printStackTrace(); } finally { try { // ■クローズ処理 // 検索結果 if(rs!=null) { rs.close();} // SQL文 if(ps!=null) { ps.close(); } // DB接続 if(conn!=null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } // 【表示値を設定する】 request.setAttribute("ryoukin", ryoukin); // 【画面に遷移する】 request.getRequestDispatcher("Newryoukin.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); // 【文字化け対応】 response.setContentType("text/html; charset=UTF-8"); response.setContentType("text/html; charset=UTF-8"); // 【画面入力値を取得し、料金情報に設定する】 // 画面入力値を取得 String course = request.getParameter("coursea_value"); int coursea = Integer.parseInt(course); // 取得できたか確認 System.out.println("coursea="+course);}

}

■JSP
<%@page contentType="text/html; charset=UTF-8" %>
<%@page import="java.sql.*"%>
<%!

%>

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>料金</title> </head> <body> <form action = "http://localhost:8080/WebApp/Seisan" metod="post"> <P>料金一覧</P> <table> <%@page contentType="text/html; charset=UTF-8" %> <table border="1"> <tr>
<th><input type="submit" value="コースA"></th> <th><input type="submit" value="コースB"></th> <th><input type="submit" value="コースC"> </th><th><input type="submit" value="OPコースA"></th> <thead> </tr> <tbody> <tr> <td>データ1</td><td>データ2</td><td>データ3</td><td>データ4</td> </tr> </table>
</thead> <input type="submit" value="ログアウト">
</table> </form>
</body> </html>

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2020/09/22 01:58

このままではコードが読みづらいので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
guest

回答2

0

ベストアンサー

こんにちは。

doGetメソッドにおいて

Java

1response.getWriter().append("Served at: ").append(request.getContextPath());

の前に

Java

1response.setContentType("text/html; charset=UTF-8");

を移動させてください。
getWriter()を呼び出す前に、setContentType()を実施する必要があります。

なお、setContentType()は一つで良いです。

投稿2020/09/22 02:43

kay-ws

総合スコア105

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

Eclipseにてサーブレットを作成した直後に出力される

java

1response.getWriter().append("Served at: ").append(request.getContextPath());

を記述してしまうと、エンコードを設定しても無視されてしまいますので削除してください。

投稿2020/09/22 02:33

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問