Servlet——Request和Response详解编程语言

Request和Response分别用来处理客户端发送的数据和服务器应该作出的响应,使用较为简单,主要都是api。读者可使用以下代码自行练习

/** 
* HttpServletRequest用来存放客户端请求的参数 
* 请求行 
* 请求头 
* 请求数据 
* 
* @author Song X. 
* @date 2020/02/11 
*/ 
public class RequestServlet extends HttpServlet {
 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
System.out.println("It is from POST Request/n"); 
showRequestInfo(request); 
} 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
System.out.println("It is from GET Request/n"); 
showRequestInfo(request); 
} 
private void showRequestInfo(HttpServletRequest request) {
 
/*以下都是请求行里的内容*/ 
System.out.println("-----------Request Line-----------"); 
//获取请求中的请求方式 
String method = request.getMethod(); 
System.out.println("Request Method: " + method); 
//获取请求的完整地址 
StringBuffer requestURL = request.getRequestURL(); 
System.out.println("Entire Request Address: " + requestURL); 
//获取请求中的资源路径 
String requestURI = request.getRequestURI(); 
System.out.println("Entire Request Path: " + requestURI); 
//获取请求中的协议与版本号 
String protocol = request.getProtocol(); 
System.out.println("Protocol: " + protocol); 
//获取请求中的协议 
String scheme = request.getScheme(); 
System.out.println("Schema: " + scheme); 
System.out.println("----------------------------------/n"); 
/*以下都是请求头里的内容*/ 
//根据Key获取对应Value的值 
System.out.println("-----------Request Header-----------"); 
//比如获取User-Agent 
String userAgent = request.getHeader("User-Agent"); 
System.out.println("User-Agent" + userAgent); 
//header中有很多的Key,可通过以下方式获取请求头信息中的所有key的枚举对象,并获取到所有的Value 
Enumeration<String> headerNames = request.getHeaderNames(); 
while (headerNames.hasMoreElements()) {
 
//headerNames.nextElement()是所有的Key 
String key = headerNames.nextElement(); 
String value = request.getHeader(key); 
System.out.println(key + " : " + value); 
} 
System.out.println("------------------------------------/n"); 
/*以下都是请求数据里的内容*/ 
System.out.println("-----------Request Data-----------"); 
//这里的参数是method.jsp里配置的,如果不一致,这里只会获取到null 
String name = request.getParameter("name"); 
String pwd = request.getParameter("pwd"); 
System.out.println(name + " : " + pwd); 
//如果不知道都有什么配置参数,可以通过如下方式获得 
Enumeration<String> parameterNames = request.getParameterNames(); 
while(parameterNames.hasMoreElements()) {
 
System.out.println(parameterNames.nextElement()); 
} 
System.out.println("----------------------------------/n"); 
} 
} 
/** 
* HttpServletResponse用来存放服务器端响应的参数 
* 响应行 
* 响应头 
* 响应体 
* 
* @author frank 
* @date 2020/02/11 
*/ 
public class ResponseServlet extends HttpServlet {
 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
System.out.println("It is from POST Request/n"); 
showResponseInfo(response); 
} 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
System.out.println("It is from GET Request/n"); 
showResponseInfo(response); 
} 
private void showResponseInfo(HttpServletResponse response) throws IOException {
 
/*以下都是响应行里的内容*/ 
//设置响应码和响应状态 
///response.sendError(404,"The Page is lost"); 
/*以下都是响应头里的内容*/ 
/* 按照Key-Value键值对的方式设置响应头,下面两种方式都可以设置响应头 
区别在于在key相同的情况下,set的方式,新的会覆盖旧的,add的方式则不会 */ 
response.setHeader("beijing","gugong"); 
response.setHeader("beijing", "yiheyuan"); 
response.addHeader("shanghai","dongfangmingzhu"); 
response.addHeader("shanghai","waitan"); 
/*以下都是响应体里的内容*/ 
//服务端给客户端回消息,并且会解析其中的html标签 
//如果不想解析其中的html标签,或者想自定义解析方式,只要设置ContentType即可(text/xxx),默认就是text/html 
//可以有比如text/palin纯文本,text/json用json解析 
response.getWriter().write("<b>Hello world</b>"); 
response.setContentType("text/html"); 
} 
} 
<%--method.jsp--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
<title>Title</title> 
</head> 
<body> 
<%--这里action的值配置的是对应跳转的Servlet的url-pattern--%> 
<form action="response" method="post"> 
用户名: <input type="text" name="name" value="" /><br> 
密码: <input type="text" name="pwd" value="" /><br> 
<input type="submit" value="登陆"> 
</form> 
</body> 
</html> 
<!--web.xml--> 
<servlet> 
<servlet-name>RequestServlet</servlet-name> 
<servlet-class>home.frank.RequestServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>RequestServlet</servlet-name> 
<url-pattern>/request</url-pattern> 
</servlet-mapping> 
<servlet> 
<servlet-name>ResponseServlet</servlet-name> 
<servlet-class>home.frank.ResponseServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>ResponseServlet</servlet-name> 
<url-pattern>/response</url-pattern> 
</servlet-mapping> 

页面输入localhost:8080/method.jsp即可测试运行

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20602.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论