Servlet(4)----HttpServletResponse/Request

servlet 跳转方式

Sendredirct  转向   

 response.sendRedirect(location);   http响应头 302 loacation:跳转地址

response.setHeader("Location", "URL");

response.setStatus(302);

forward  转发    

通知服务器去调用web资源

加载跳转页面返回  http响应头 200

request.setAttribute("usr", "u");

//表示使用转发的方法,把request和response对象传递给下一个Servlet

//地址不用带web应用名

request.getRequestDispatcher("/Servlet2").forward(request, response);

Servlet2:

request.getAttribute("usr");

设置header头
//跳转到下一个页面

response.setHeader("Location", "URL");

response.setStatus(302);


下载文件头

response.setHeader("Content-Disposition","attachment;filename=timg.jpg");

获取下载文件的全路径

this.getServletContext().getRealPath("/images/timg.jpg");


response输出流

ServletOutputStream os = response.getOutputStream();



HttpServletResponse

getWriter()              输出字符数据

getOutputStream()   输出二进制数据



HttpServletRequest

StringBuffer requestURL = request.getRequestURL();

String requestURI = request.getRequestURI();

String queryString = request.getQueryString();     参数名+值

String remoteAddr = request.getRemoteAddr();  获取请求方的IP地址

String remoteHost = request.getRemoteHost();  获取请求方的主机名

int port = request.getRemotePort();         获取客户端的端口

int serverport = request.getLocalPort();   获取服务器的端口

request.getHeader("");        获取请求头

EnumerationheaderNames = request.getHeaderNames();  获取所有的请求头名

String parameter = request.getParameter("");   获取表单提交的值

String[] parameterValues = request.getParameterValues("");   获取表单复选框内容

你可能感兴趣的:(Servlet(4)----HttpServletResponse/Request)