本文主要是介绍JSPSERVLET学习笔记(六):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>");
}
}
这篇关于JSPSERVLET学习笔记(六):getReader(), getInputStream()读取Body内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!