protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 响应行相关的方法
response.sendError(404,"请求资源不存在");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 响应消息头相关的方法
// 在用户名和密码输入错误的情况下,重新回到登录页面让用户重新输入数据
// 1.设置状态码
response.setStatus(302);
// 2.设置响应头,完成重定向到登录页面
response.setHeader("Location", "/chapter/login.html");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 响应体相关的方法
// 字节流对象,向浏览器返回数据
ServletOutputStream out = response.getOutputStream();
String value = "web";
out.write(value.getBytes());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 响应体相关的方法
// 字符流向浏览器输出数据
PrintWriter pWriter = response.getWriter();
String value = "web----";
pWriter.write(value);
}
package chapter.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ChineseServlet
*/
public class ChineseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.处理字节流的中文乱码问题
String data = "中国";
ServletOutputStream out = response.getOutputStream();
// 2.处理乱码:服务器端的编码和浏览器端的编码保持一致
// 3.让浏览器端的编码也是utf-8
response.setHeader("Content-Type", "text/html;charset=utf-8");
//response.setContentType("text/html;charset=utf-8");
// 4.让服务器端的中文编码格式是utf-8
out.write(data.getBytes("utf-8"));
}
/**
* @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.setHeader("Content-Type", "text/html;charset=utf-8");
response.setContentType("text/html;charset=utf-8");
package chapter.response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ChineseServlet
*/
public class ChineseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.处理字符流的中文乱码问题
// 2.处理服务器的编码,让这个编码支持中文
response.setCharacterEncoding("utf-8");
// 3.处理器浏览器端的编码
response.setHeader("Content-Type", "text/html;charset=utf-8");
//response.setContentType("text/html;charset=GB18030");
String data = "中国";
// 4.获取字符流
PrintWriter pw = response.getWriter();
pw.print(data);
}
/**
* @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.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setContentType("text/html;charset=GB18030");
注:在处理字符流和字节流输出中文乱码时,都采用:response.setContentType("text/html;charset=utf-8");
常见的编码格式:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.定时跳转:需要用到Refresh响应头
response.setHeader("Refresh", "5;URL=/chapter/login.html");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 定时刷新
response.setHeader("Refresh", "5");
response.getWriter().print(new Date());
}