老师,乱码问题,改了半天也不行。麻烦老师帮看下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <body> <form action= "/JDBCAndJSP/add" method= "get" > <h2>课程添加</h2> <table> <tr> <td>课程名称:<input type= "text" name= "name" ></td> </tr> <tr> <td>所属方向:<input type= "text" name= "category" ></td> </tr> <tr> <td>课程描述:<input type= "text" name= "scrp" ></td> </tr> <tr><td><input type= "submit" value= "添加" ></td></tr> </table> </form> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %> <%@ taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <body> <table> <tr> <td style= "font-size:30px" >课程名称</td> <td style= "font-size:30px" >所属方向</td> <td style= "font-size:30px" >课程描述</td> <td style= "font-size:30px" >创建时间</td> </tr> <c:forEach var= "course" items= "${courses }" varStatus= "ind" > <tr> <td>${course.name }</td> <td>${course.category }</td> <td>${course.desp }</td> <td><fmt:formatDate value= "${course.createTime }" pattern= "hh小时mm分钟ss秒" ></fmt:formatDate></td> </tr> </c:forEach> </table> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.imooc.servlet; import java.io.IOException; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; 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 Utils.JDBCUtials; import dto.Dto; /** * Servlet implementation class addServlet */ @WebServlet ( "/add" ) public class addServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public addServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter( "name" ); String category = request.getParameter( "category" ); String desp = request.getParameter( "scrp" ); System.out.println(name); System.out.println(category); System.out.println(desp); Dto dto = new Dto(); boolean flag = dto.add(name, category, desp); if (flag) { response.sendRedirect( "/JDBCAndJSP/show" ); } else { System.out.println( "添加失败了" ); } } /** * @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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; 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 dto.Dto; import entity.Course; /** * Servlet implementation class ShowServlet */ @WebServlet ( "/show" ) public class ShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowServlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "utf-8" ); response.setContentType( "text/html; charset=utf-8" ); ServletContext context = request.getServletContext(); if (context.getAttribute( "courses" ) == null ) { Dto dto = new Dto(); List<Course>courses = new ArrayList<>(); courses = dto.show(); for (Course c : courses) { System.out.println(c.getId()); System.out.println(c.getName()); System.out.println(c.getCategory()); System.out.println(c.getCreateTime()); System.out.println(c.getDesp()); } context.setAttribute( "courses" , courses); } request.getRequestDispatcher( "showCourse.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); } } |
下边是工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package jdbcutils; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JDBCUtils { private static final String driver; private static final String url; private static final String user; private static final String password; static { Properties prop = new Properties(); try { InputStream is = JDBCUtils. class .getClassLoader().getResourceAsStream( "driver.properties" ); prop.load(is); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } driver=prop.getProperty( "driver" ); url = prop.getProperty( "url" ); user = prop.getProperty( "username" ); password = prop.getProperty( "password" ); } //注册驱动 public static void driverLoad() throws ClassNotFoundException { Class.forName(driver); } //获取连接 public static Connection getConnection (Connection conn) throws Exception { driverLoad(); conn = DriverManager.getConnection(url, user, password); return conn; } //释放资源 public static void release(Connection conn, Statement stmt) { if (conn != null ) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } conn = null ; if (stmt != null ) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } stmt = null ; } public static void release(Connection conn, Statement stmt, ResultSet rs) { if (conn != null ) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } conn = null ; if (stmt != null ) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }; } stmt = null ; if (rs != null ) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } rs = null ; } } |
1 | <br><br> |
下边是工具类的配置文件driver.properties
1 2 3 4 | root=root password= 123456 url=jdbc:mysql: //127.0.0.1:3306/jdbcdemo?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8 driver=com.mysql.cj.jdbc.Driver |
下边是course类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package entity; import java.util.Date; public class Course { private Integer id; private String name; private String category; private String desp; private Date createTime; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getCategory() { return category; } public void setCategory(String category) { this .category = category; } public String getDesp() { return desp; } public void setDesp(String desp) { this .desp = desp; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this .createTime = createTime; } public Course(Integer id, String name, String category, String desp, Date createTime) { super (); this .id = id; this .name = name; this .category = category; this .desp = desp; this .createTime = createTime; } public Course() { } } |
我是tomcat7 我把tomcat/conf/server.xml文件尾部也加上了URIEncoding="UTF-8"
1 | < Connector connectionTimeout = "20000" port = "8080" protocol = "HTTP/1.1" redirectPort = "8443" URIEncoding = "UTF-8" /> |
但是还是不行
0
收起
正在回答 回答被采纳积分+1
2回答
相似问题
登录后可查看更多问答,登录/注册
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧