Servlet第四篇Request常用方法
Posted kwdlh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet第四篇Request常用方法相关的知识,希望对你有一定的参考价值。
防盗链
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String referer = req.getHeader("Referer");
if(referer==null|| !"http://localhost:8080/a".equals(referer)){
PrintWriter writer = resp.getWriter();
writer.write("防盗链");
}else{
PrintWriter writer = resp.getWriter();
writer.write("路飞在跑");
}
}
请求转发
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.write("a");
req.getRequestDispatcher("/b").forward(req,resp);
}
RequestDispatcher的include方法
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
req.getRequestDispatcher("/c").include(req, resp);//匹配的Servlet代码writer.write("c");
writer.write("a");
req.getRequestDispatcher("/d").include(req, resp);//匹配的Servlet代码writer.write("d");
}
//页面显示:cad
以上是关于Servlet第四篇Request常用方法的主要内容,如果未能解决你的问题,请参考以下文章