重定向:我给服务器请求一条消息,服务器让我访问另外一个服务器(新的地址)。302状态码,并设置location头,完成重定向。两个请求。
package com.pcx.servlets;
import java.io.File; import java.io.FileInputStream; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
/** * @author 平常心cz *测试io流 */ public class Gservlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 发送文字 */ // String s="hello Response"; // byte[] bytes=s.getBytes(); // // response.getOutputStream().write(bytes);
/** * 发送图片 */ String path ="D:/a.jpg"; FileInputStream in=new FileInputStream(new File(path)); byte[] bytes=IOUtils.toByteArray(in); response.getOutputStream().write(bytes); }
} |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 设置重定向 * 1设置location * 2设置302状态码 */ // response.setHeader("Location", "/zuoye/hello100.jsp"); // response.setStatus(302);
/** * 快捷的重定向 */ response.sendRedirect("http:www.baidu.com"); } |
Request
/* * 适用referer 来防盗链 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String referer=request.getHeader("Referer"); System.out.println(referer); if (referer==null||!(referer.contains("localhost"))) { System.out.println("hello"); }else { response.sendRedirect("http://www.baidu.com"); } } |
/** * @author 平常心cz *测试request的获取url */ public class Dservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().print(request.getScheme()+"<br/>");//获取请求协议 response.getWriter().print(request.getServerName()+"<br/>");//获取服务器的名称 response.getWriter().print(request.getServerPort()+"<br/>");//获取服务器的端口号 response.getWriter().print(request.getServletContext()+"<br/>");//获取项目名 response.getWriter().print(request.getServletPath()+"<br/>");//获取servlet的路径 response.getWriter().print(request.getQueryString()+"<br/>");//获取请求的参数 response.getWriter().print(request.getRequestURI()+"<br/>");//获取请求的uri response.getWriter().print(request.getRequestURL()+"<br/>");//获取请求的url }
} |
package com.pcx.servlets;
import java.io.IOException; import java.util.Arrays; import java.util.Enumeration; import java.util.Map;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * @author 平常心cz *测试request获取请求参数 */ public class Fservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("xxx")); System.out.println(request.getParameter("yyy")); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); String[] hobbys=request.getParameterValues("hobby"); System.out.println(username+" "+password+" "+Arrays.toString(hobbys));
/** * 测试获取多个参数名称 */ Enumeration names=request.getAttributeNames(); while(names.hasMoreElements()){ System.out.println(names.nextElement()); }
/** * 获取所有的参数 并封装到map中 */ Map<String, String[]>map=request.getParameterMap(); System.out.println(map); for (String name : map.keySet()) { String [] values=map.get(name); System.out.println(name+"="+Arrays.toString(values)); } }
} |