目录
一、response响应
response对象用于响应客户请求并向客户端输出信息
二、response应用
1、 response主要方法
2、代码演示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取数据</title>
</head>
<body>
<form action="RequestServlet" method="post">
<p><label>账号:</label><input type="text" name="user"></p>
<p><label>密码:</label><input type="password" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
</body>
</html>
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
System.out.println("user:"+user+"\npassword:"+password);
//获取输出流
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println("<head><title>login success</title></head>");
printWriter.println("<body>");
printWriter.println("<h2>amazing!</h2>");
printWriter.println("</body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3、解决中文输出乱码
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取输出流
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println("<head><title>login success</title></head>");
printWriter.println("<body>");
printWriter.println("<h2>纹身噶,黑社会?!</h2>");
printWriter.println("</body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
如果输出内容包含中文,则出现乱码,因为服务器默认采用ISO8859-1编码响应内容
(1)解决方式一
设置服务器端响应的编码格式和设置客户端响应内容的头内容的文件类型及编码格式,这种方式不推荐
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type","text/html;charset=utf-8");
//获取输出流
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println("<head><title>login success</title></head>");
printWriter.println("<body>");
printWriter.println("<h2>纹身噶,黑社会?!</h2>");
printWriter.println("</body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(2)解决方式二
同时设置服务端的编码格式和客户端响应的文件类型及响应时的编码格式,推荐此方式
package com.java.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "RequestServlet", value = "/RequestServlet")
public class RequestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
response.setContentType("text/html;charset=utf-8");
//获取输出流
PrintWriter printWriter = response.getWriter();
printWriter.println("<html>");
printWriter.println("<head><title>login success</title></head>");
printWriter.println("<body>");
printWriter.println("<h2>纹身噶,黑社会?!</h2>");
printWriter.println("</body>");
printWriter.println("</html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}