teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

サーブレットの内容

2020/09/22 01:58

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,79 +1,166 @@
1
1
  下記の通りにサーバーで実行したら文字化けが発生してしまいます。どなたか改善策があれば知恵をお貸しいただきたいです。
2
2
 
3
3
  ■Servlet
4
- package ryoukin.java;
4
+ package seisan;
5
5
 
6
+ import java.io.IOException;
7
+ import java.sql.Connection;
8
+ import java.sql.DriverManager;
9
+ import java.sql.PreparedStatement;
10
+ import java.sql.ResultSet;
11
+ import java.sql.SQLException;
6
12
 
7
- public class ryoukin_beans {
13
+ import javax.annotation.Resource;
14
+ import javax.servlet.ServletException;
15
+ import javax.servlet.annotation.WebServlet;
16
+ import javax.servlet.http.HttpServlet;
17
+ import javax.servlet.http.HttpServletRequest;
18
+ import javax.servlet.http.HttpServletResponse;
19
+ import javax.sql.DataSource;
8
20
 
9
- private int coursea_value;
10
- private int courseb_value;
11
- private int coursec_value;
12
- private int opcoursea_value;
21
+ import ryoukin.java.ryoukin_beans;
13
- private int opcourseb_value;
14
- private int opcoursec_value;
15
- public ryoukin_beans(int coursea_value, int courseb_value, int coursec_value, int opcoursea_value,
16
- int opcourseb_value, int opcoursec_value) {
17
- super();
18
- this.coursea_value = coursea_value;
19
- this.courseb_value = courseb_value;
20
- this.coursec_value = coursec_value;
21
- this.opcoursea_value = opcoursea_value;
22
- this.opcourseb_value = opcourseb_value;
23
- this.opcoursec_value = opcoursec_value;
24
- }
25
- public int getCoursea_value() {
26
- return coursea_value;
27
- }
28
- public void setCoursea_value(int coursea_value) {
29
- this.coursea_value = coursea_value;
30
- }
31
- public int getCourseb_value() {
32
- return courseb_value;
33
- }
34
- public void setCourseb_value(int courseb_value) {
35
- this.courseb_value = courseb_value;
36
- }
37
- public int getCoursec_value() {
38
- return coursec_value;
39
- }
40
- public void setCoursec_value(int coursec_value) {
41
- this.coursec_value = coursec_value;
42
- }
43
- public int getOpcoursea_value() {
44
- return opcoursea_value;
45
- }
46
- public void setOpcoursea_value(int opcoursea_value) {
47
- this.opcoursea_value = opcoursea_value;
48
- }
49
- public int getOpcourseb_value() {
50
- return opcourseb_value;
51
- }
52
- public void setOpcourseb_value(int opcourseb_value) {
53
- this.opcourseb_value = opcourseb_value;
54
- }
55
- public int getOpcoursec_value() {
56
- return opcoursec_value;
57
- }
58
- public void setOpcoursec_value(int opcoursec_value) {
59
- this.opcoursec_value = opcoursec_value;
60
- }
61
- @Override
62
- public String toString() {
63
- return "ryoukin_beans [coursea_value=" + coursea_value + ", courseb_value=" + courseb_value + ", coursec_value="
64
- + coursec_value + ", opcoursea_value=" + opcoursea_value + ", opcourseb_value=" + opcourseb_value
65
- + ", opcoursec_value=" + opcoursec_value + "]";
66
- }
67
- public void set(int coursea) {
68
- // TODO 自動生成されたメソッド・スタブ
69
-
70
- }
71
22
 
23
+ /**
24
+ * Servlet implementation class SeisanServlet
25
+ */
26
+ @WebServlet("/Seisan")
27
+ public class SeisanServlet extends HttpServlet {
28
+ private static final long serialVersionUID = 1L;
72
29
 
30
+ // Define datasource/connection pool for Resource Injection
31
+ @Resource(name="jdbc/WebApp")
32
+ private DataSource dataSource;
33
+
34
+ /**
35
+ * @see HttpServlet#HttpServlet()
36
+ */
37
+ public SeisanServlet() {
38
+ super();
39
+ // TODO Auto-generated constructor stub
40
+ }
41
+
42
+ /**
43
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
44
+ */
45
+ @SuppressWarnings("unused")
46
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47
+ // TODO Auto-generated method stub
48
+ response.getWriter().append("Served at: ").append(request.getContextPath());
49
+ //文字化け対策
50
+ response.setContentType("text/html; charset=UTF-8");
51
+ response.setContentType("text/html; charset=UTF-8");
52
+ // 【料金情報を取得する】
53
+ ryoukin_beans ryoukin = null;
54
+
55
+ // ■DB接続
56
+ Connection conn = null;
57
+ PreparedStatement ps = null;
58
+ ResultSet rs = null;
59
+
60
+ // 接続情報
61
+ String url="jdbc:mariadb://localhost:3306/webApp";
62
+ String user="user02";
63
+ String password="user02";
64
+
65
+ try {
66
+ // 接続処理
67
+ conn = DriverManager.getConnection(url, user, password);
68
+
69
+ // ■検索処理実行
70
+ // SQL作成
71
+ String sql = "SELECT coursea_value, courseb_value, coursec_value,opcoursea_value FROM seisan";
72
+ System.out.println(sql);
73
+
74
+ // SQL準備
75
+ ps = conn.prepareStatement(sql);
76
+
77
+ // SQL実行
78
+ rs = ps.executeQuery();
79
+
80
+ // データを読込み、JavaBeansに設定する
81
+ ryoukin = new ryoukin_beans(0, 0, 0, 0, 0, 0);
82
+ while(rs.next()) {
83
+ // データ読込
84
+ int coursea = rs.getInt("coursea_value");
85
+ int courseb = rs.getInt("courseb_value");
86
+ int coursec = rs.getInt("coursec_value");
87
+ int opcoursea= rs.getInt("opcoursea_value");
88
+
89
+
90
+
91
+ // JavaBeansにデータを設定
92
+ ryoukin.set(coursea);
93
+ ryoukin.set(courseb);
94
+ ryoukin.set(coursec);
95
+ ryoukin.set(opcoursea);
96
+
97
+
98
+
99
+ }
100
+ System.out.println(ryoukin);
101
+ } catch(SQLException e) {
102
+ // 接続失敗時の処理
103
+ e.printStackTrace();
104
+
105
+ } finally {
106
+ try {
107
+ // ■クローズ処理
108
+ // 検索結果
109
+ if(rs!=null) {
110
+ rs.close();}
111
+
112
+ // SQL文
113
+ if(ps!=null) {
114
+ ps.close();
115
+ }
116
+ // DB接続
117
+ if(conn!=null) {
118
+ conn.close();
119
+ }
120
+ } catch (SQLException e) {
121
+ e.printStackTrace();
122
+ }
123
+ }
124
+
125
+ // 【表示値を設定する】
126
+ request.setAttribute("ryoukin", ryoukin);
127
+
128
+ // 【画面に遷移する】
129
+ request.getRequestDispatcher("Newryoukin.jsp").forward(request, response);
130
+ }
131
+
132
+
133
+
134
+ /**
135
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
136
+ */
137
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
138
+ // TODO Auto-generated method stub
139
+ doGet(request, response);
140
+ // 【文字化け対応】
141
+ response.setContentType("text/html; charset=UTF-8");
142
+ response.setContentType("text/html; charset=UTF-8");
143
+
144
+ // 【画面入力値を取得し、料金情報に設定する】
145
+ // 画面入力値を取得
146
+ String course = request.getParameter("coursea_value");
147
+ int coursea = Integer.parseInt(course);
148
+
149
+
150
+
151
+ // 取得できたか確認
152
+ System.out.println("coursea="+course);}
73
153
  }
74
154
 
75
155
 
76
156
 
157
+
158
+
159
+
160
+
161
+
162
+
163
+
77
164
  ■JSP
78
165
  <%@page contentType="text/html; charset=UTF-8" %>
79
166
  <%@page import="java.sql.*"%>