お世話になります。
java初心者でようやく会員登録フォームからデータベースに登録するというところまで来たのですが、insert文でinsertしたデータの日本語が文字化けしてしまいます。
調べてみると、setcharaEncodingあたりで不具合が出ているのだと思うのですが、お分かりになる方、回答のほど、よろしくお願いいたします。
java
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5<html> 6<head> 7<title>会員登録フォーム</title> 8</head> 9<body> 10 11<H1>会員登録フォーム</H1> 12<P>ユーザーID</P> 13<FORM ACTION="../InsertDb" method="get"> 14<INPUT type="text" name="user"> 15<P>パスワード</P> 16<INPUT type="password" name="pass"> 17<P>年齢</P> 18<INPUT type="text" name="age"> 19<INPUT type="reset"> 20<INPUT type="submit"> 21</FORM> 22</body> 23</html>
java
1package insert; 2 3import java.io.IOException; 4import java.sql.PreparedStatement; 5import java.sql.SQLException; 6 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import com.mysql.jdbc.Connection; 14 15import connect.MySql; 16 17 18@WebServlet("/InsertDb") 19public class InsertDb extends HttpServlet 20{ 21 private static final long serialVersionUID = 1L; 22 23 protected void doGet(HttpServletRequest request, 24 HttpServletResponse response) throws ServletException, IOException 25 { 26 Connection con = null; 27 PreparedStatement ps = null; 28 String user = request.getParameter("user"); 29 String pass = request.getParameter("pass"); 30 String ages = request.getParameter("age"); 31 32 int pass1 = Integer.parseInt(pass); 33 int age1 = Integer.parseInt(ages); 34 35 36 37 38 39 try 40 { 41 // MySQLに接続 42 con = MySql.getConnection(); 43 44 String sql = "INSERT INTO PERSONAL(username,password,age) VALUES(?,?,?)"; 45 46 ps = con.prepareStatement(sql); 47 ps.setString(1, user); 48 ps.setInt(2, pass1); 49 ps.setInt(3, age1); 50 int num = ps.executeUpdate(); 51 System.out.println(num); 52 53 } 54 catch(SQLException e) 55 { 56 throw new ServletException(e); 57 } 58 finally 59 { 60 if(ps != null) 61 { 62 try 63 { 64 ps.close(); 65 } 66 catch(SQLException ignore) 67 {} 68 } 69 if(con != null) 70 { 71 try 72 { 73 con.close(); 74 } 75 catch(SQLException ignore) 76 {} 77 } 78 } 79 request.getRequestDispatcher("/db/test.jsp") 80 .forward(request, response); 81 82 } 83 84} 85
あと、filterでencodingの設定をしているはずなので、
一応、これも載せておきます。
java
1package filter; 2 3import javax.servlet.Filter; 4import javax.servlet.FilterChain; 5import javax.servlet.FilterConfig; 6import javax.servlet.ServletException; 7import javax.servlet.ServletRequest; 8import javax.servlet.ServletResponse; 9import javax.servlet.annotation.WebFilter; 10 11@WebFilter(filterName="EncodingFilter", urlPatterns="/*") 12public class EncodingFilter implements Filter 13{ 14 public void init(FilterConfig fConfig) throws ServletException 15 { 16 } 17 public void doFilter(ServletRequest request, 18 ServletResponse response, 19 FilterChain chain) 20 throws java.io.IOException, ServletException 21 { 22 // place your code here 23 request.setCharacterEncoding("UTF-8"); 24 System.out.println("aiu"); 25 // pass the request along the filter chain 26 chain.doFilter(request, response); 27 } 28 public void destroy(){} 29}
回答1件
あなたの回答
tips
プレビュー