JSP&&SERVLET学习笔记(六):getReader(), getInputStream()读取Body内容

这个Servlet内的方法可读取网页的内部信息


package cc.openhome;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/body.view")
public class BodyServlet extends HttpServlet {
 
private String readBody(HttpServletRequest request) throws IOException{
BufferedReader reader = request.getReader();
String input = null;
String requestBody = "";
while((input = reader.readLine()) != null){
requestBody = requestBody + input + "<br>";
}
return requestBody;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String body = readBody(request);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BodyView</title>");
out.println("</head>");
out.println("<body>");
out.println(body);
out.println("</body>");
out.println("</html>");
}
}

你可能感兴趣的:(JSP&&SERVLET学习笔记(六):getReader(), getInputStream()读取Body内容)