页面编码的问题,resp.setContentType("text/html");resp.setCharacterEncoding("charset=gb2312");会报错
packagecom.zph.test.bean;importjava.io.IOException;importjava.io.PrintWriter;importja...
package com.zph.test.bean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowRs extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
/*
* 这里出错没搞明白,页面不能转化为中文
resp.setContentType("text/html");
resp.setCharacterEncoding("charset=gb2312");
*/
PrintWriter pw = resp.getWriter();
pw.println("<html><head><title></title></head>"
+ "<body bgcolor='#FFFF66'>\n"
+ "<table border=1>\n"
+ "<tr><td>title:</td></tr>\n");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs?userUnicode=true&characterEncoding=utf-8","root","");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from article");
while(rs.next()){
pw.println("<tr>");
pw.println("<td>" + rs.getString("title") + "</td>");
pw.println("</tr>");
}
pw.println("</table>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
} if (stmt != null) {
stmt.close();
stmt = null;
} if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
pw.println("</body></html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
} 展开
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowRs extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
/*
* 这里出错没搞明白,页面不能转化为中文
resp.setContentType("text/html");
resp.setCharacterEncoding("charset=gb2312");
*/
PrintWriter pw = resp.getWriter();
pw.println("<html><head><title></title></head>"
+ "<body bgcolor='#FFFF66'>\n"
+ "<table border=1>\n"
+ "<tr><td>title:</td></tr>\n");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs?userUnicode=true&characterEncoding=utf-8","root","");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from article");
while(rs.next()){
pw.println("<tr>");
pw.println("<td>" + rs.getString("title") + "</td>");
pw.println("</tr>");
}
pw.println("</table>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
} if (stmt != null) {
stmt.close();
stmt = null;
} if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
pw.println("</body></html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询