欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Web基础之Request对象与Response对象

程序员文章站 2022-06-11 16:56:10
...

Web基础之Request对象与Response对象

一、Response对象:

Response 响应对象,参考响应报文,它应该有哪些功能?
响应首行
协议/版本号
状态码/描述 (api中搜索status)
响应头
添加响应头参数  (api中搜索header)
响应正文
发送字节/字符流 (api中搜索stream,writer)
Web基础之Request对象与Response对象


二、Response对象的使用:Response对象主要用于输出响应;

结合API实现状态码的设置,乱码处理以及重定向实例;

整体项目结构:
Web基础之Request对象与Response对象

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JumpServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		resp.setStatus(302);
//		resp.setHeader("Location", "http://www.taobao.com");
		resp.sendError(404, "就是找不到呀.......");
	}
}


package com.hx.servlet02;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LuanCodeServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		resp.setContentType("text/html;charset=utf-8");
//		String contentType=req.getServletContext().getMimeType(".html");
//		resp.addHeader("Content-Type", contentType+";charset=UTF-8");
		resp.getWriter().write("举杯邀明月,对影成三人!");
	}
}


package com.hx.servlet02;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RefreshServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.addHeader("Refresh", "9,http://www.jd.com");
		resp.setContentType("text/html;charset=UTF-8");
		StringBuilder builder=new StringBuilder();
		builder.append("<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>10之后访问京东</title></head><body>")
		.append("<p><center>您将在<span id=\"myspan\" style=\"color:red\">10</span>秒之后访问京东!</center></p></body>")
		.append("<script type=\"text/javascript\">var i=10;setInterval(function(){")
		.append("var myspan=document.getElementById(\"myspan\");i--;myspan.style.color='red';")
		.append("myspan.innerHTML=i+\"\";")
		.append("}, 1000);</script></html>");
		resp.getWriter().write(builder.toString());
	}
}



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>10之后访问京东</title>
</head>
<body>
	<p>
	<center>
		您将在<span id="myspan" style="color: red">10</span>秒之后访问京东!
	</center>
	</p>
</body>
<script type="text/javascript">
	var i = 10;
	setInterval(function() {
		var myspan = document.getElementById("myspan");
		i--;
		myspan.style.color = 'red';
		myspan.innerHTML = i + "";
		if (i == 2) {
			window.setTimeout("window.location='http://www.jd.com'", 1000)
		}
	}, 1000);
</script>
</html>


xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Servlet02</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>jumpServlet</servlet-name>
  <servlet-class>com.hx.servlet02.JumpServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>jumpServlet</servlet-name>
  <url-pattern>/jump</url-pattern>
  </servlet-mapping>
  
  
  <servlet>
  <servlet-name>luanCodeServlet</servlet-name>
  <servlet-class>com.hx.servlet02.LuanCodeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>luanCodeServlet</servlet-name>
  <url-pattern>/luancode</url-pattern>
  </servlet-mapping>
  
  
  <servlet>
  <servlet-name>refreshServlet</servlet-name>
  <servlet-class>com.hx.servlet02.RefreshServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>refreshServlet</servlet-name>
  <url-pattern>/refresh</url-pattern>
  </servlet-mapping>
  
</web-app>


Web基础之Request对象与Response对象

乱码问题:(要去除乱码,可以恢复注释的内容)

Web基础之Request对象与Response对象


十秒访问京东网站首页:

Web基础之Request对象与Response对象




三、Request对象:

Request 请求对象,参考响应报文,它应该有哪些功能?
请求首行
请求方式
请求路径
协议/版本号
请求头
获取头:获取原始头是一行一行的信息,是否有封装的方法
请求正文
获取整个正文,还需要去截取字符串,是否有封装的方法

Web基础之Request对象与Response对象


Request获取参数的其他方法:

Web基础之Request对象与Response对象




四、Request对象的使用:Request对象主要用于获取参数以及保存参数值

一个小案例:

Web基础之Request对象与Response对象


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
	<form action="login" method="POST">
		<input type="text" name="username"/> <br/>
		<input type="password" name="password"/> <br/>
		<input type="submit" value="登录"> <br/>
	</form>
</body>
</html>

package com.usc.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String userName=req.getParameter("username");
		
		String password =req.getParameter("password");
		
		System.out.println("姓名:"+userName);
		System.out.println("密码:"+password);
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  <servlet>
  <servlet-name>Login</servlet-name>
  <servlet-class>com.usc.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>Login</servlet-name>
  <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

Web基础之Request对象与Response对象

Web基础之Request对象与Response对象


这里出现的乱码问题我们等会解决;


Request转发请求:

Web基础之Request对象与Response对象

转发:一个Servlet处理完毕交给下面的servlet(JSP)继续处理。开发过程中,如果在Servlet中输出HTML,会很混乱。为了分工明确,通常Servlet只负责处理业务逻辑,那么显示部分由谁处理呢?JSP

另外:需要做重定向,或者转发的Servlet,都不使用输出流输出。没有效果 ,放在前面还容易导致乱码。


Request请求保存参数的作用域:

Web基础之Request对象与Response对象



五、乱码问题:


乱码出现的问题在于服务端转化编码与客户端解析编码不一致,只要两边使用的编码一致,则不会出现乱码问题!


Web基础之Request对象与Response对象


浏览器如何解析收到的响应正文?两种方式。
响应头中配置:Context-Type:text/html;charset=utf-8;


服务端如何转化发送的响应正文?
字节流: "响应正文".getBytes("utf-8");
字符流:   response.setCharacterEncoding("utf-8");
Web基础之Request对象与Response对象

Web基础之Request对象与Response对象


Web基础之Request对象与Response对象



Web基础之Request对象与Response对象


Web基础之Request对象与Response对象



Web基础之Request对象与Response对象


六、总结
1、要对HTTP协议有一定的了解;
2、Response对象主要负责处理响应,主要可以设置响应状态码,设置响应头等信息;
3、Request对象主要负责处理请求,主要的作用是获取参数以及将数据保存到Request作用于中;
4、由于客户端和服务器两边的编码不一样,所以会出现乱码问题;要解决乱码问题,需要从根本上了解乱码出现的原因;然后加以解决;’
共勉!