如何使用Servlet处理客户端的请求及响应
1个回答
展开全部
如何使用Servlet从客户端获取请求信息:
首先,让我们先来看一个请求的实例:
GET/default.asp?username=zhangsan HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/png, */* Accept-Language: en Connection: Keep-Alive Host: ww w.n eusoft.ed u.c n
User-Agent: Mozilla/4.04 [en] (WinNT; I Nav)
Cookie:SITESERVER=ID=8dac8e0455f4890da220ada8b76f; ASPSESSIONIDGGQGGGAF=JLKHAEICGAHEPPMJKMLDEM Accept-Charset: iso-8859-1,*,utf-8
从以上请求信息中,我们可以看出请求信息大致可以分成这样几类:
第一类协议相关的信息,如使用的请求方法、请求使用的协议以及请求的资源地址等信息 第二类是请求头信息,如Accept以及Accept-language等等头部信息 第三类信息是用户组织的数据如username=zhangsan等信息 因此从服务器的角度必须能够获取以上信息,也就是通过请求对象对应的方法要能够获取响应的请求信息
第一类请求方法,获取协议相关的信息的方法: getProtocal():获取协议名称以及版本信息 getMethod();获取Http请求所使用的方法 getRequestURI()获取请求的URI getRequestURL()获取请求的URL
下面我们看一个获取协议信息的示例: package neusoft;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class RequestServlet */
@WebServlet("/r1")
public class RequestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet() */ public RequestServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("</head>"); out.print("<body>"); out.print("请求使用的协议为:" + request.getProtocol() + "<br>"); out.print("请求提交数据所使用的方法为:" + request.getMethod() + "<br>"); out.print("请求的URI为:" + request.getRequestURI() + "<br>"); out.print("请求的URL为:" + request.getRequestURL() + "<br>"); out.print("</body>"); out.print("</html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response);// TODO Auto-generated method stub } }
第二类获取请求头信息相关的方法:
getHeader(String headerName)获取指定的头部信息,返回字符串类型 getDateHeader(String headerName)获取指定的头部信息,返回日期类型 getIntHeader获取指定的头部信息,返回整数类型
getHeaders获取指定的头部信息,返回字符串数组类型 getDateHeaders获取指定的头部信息,返回字日期数组类型 getIntHeaders获取指定的头部信息,返回字整数数组类型 getHeaderNames()获取客户端传递过来所有头部的名称 Http1.1协议中定义的头部信息如下:
通用头部信息(即请求和响应均可以使用的头部信息) general-header = Cache-Control
| Connection | Date | Pragma | Trailer | Transfer-Encoding | Upgrade | Via | Warning 除此之外还有请求专用的请求头:
request-header = Accept
| Accept-Charset | Accept-Encoding | Accept-Language | Authorization | Expect | From | Host | If-Match | If-Modified-Since | If-None-Match | If-Range | If-Unmodified-Since | Max-Forwards | Proxy-Authorization | Range
| Referer | TE | User-Agent
例如我们想判断用户浏览器的类型以及所使用的地区语言可以使用如下语句: package neusoft;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class RequestServlet2 */
@WebServlet("/r2")
public class RequestServlet2 extends HttpServlet { private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet() */
public RequestServlet2() { super();
// TODO Auto-generated constructor stub }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("</head>"); out.print("<body>"); out.print("您使用的浏览器信息为:" + request.getHeader("User-Agent")+ "<br>"); out.print("您使用的地区语言为" + request.getHeader("Accept-Language") + "<br>"); out.print("</body>"); out.print("</html>");
} /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response);// TODO Auto-generated method stub }
首先,让我们先来看一个请求的实例:
GET/default.asp?username=zhangsan HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/png, */* Accept-Language: en Connection: Keep-Alive Host: ww w.n eusoft.ed u.c n
User-Agent: Mozilla/4.04 [en] (WinNT; I Nav)
Cookie:SITESERVER=ID=8dac8e0455f4890da220ada8b76f; ASPSESSIONIDGGQGGGAF=JLKHAEICGAHEPPMJKMLDEM Accept-Charset: iso-8859-1,*,utf-8
从以上请求信息中,我们可以看出请求信息大致可以分成这样几类:
第一类协议相关的信息,如使用的请求方法、请求使用的协议以及请求的资源地址等信息 第二类是请求头信息,如Accept以及Accept-language等等头部信息 第三类信息是用户组织的数据如username=zhangsan等信息 因此从服务器的角度必须能够获取以上信息,也就是通过请求对象对应的方法要能够获取响应的请求信息
第一类请求方法,获取协议相关的信息的方法: getProtocal():获取协议名称以及版本信息 getMethod();获取Http请求所使用的方法 getRequestURI()获取请求的URI getRequestURL()获取请求的URL
下面我们看一个获取协议信息的示例: package neusoft;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class RequestServlet */
@WebServlet("/r1")
public class RequestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet() */ public RequestServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("</head>"); out.print("<body>"); out.print("请求使用的协议为:" + request.getProtocol() + "<br>"); out.print("请求提交数据所使用的方法为:" + request.getMethod() + "<br>"); out.print("请求的URI为:" + request.getRequestURI() + "<br>"); out.print("请求的URL为:" + request.getRequestURL() + "<br>"); out.print("</body>"); out.print("</html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response);// TODO Auto-generated method stub } }
第二类获取请求头信息相关的方法:
getHeader(String headerName)获取指定的头部信息,返回字符串类型 getDateHeader(String headerName)获取指定的头部信息,返回日期类型 getIntHeader获取指定的头部信息,返回整数类型
getHeaders获取指定的头部信息,返回字符串数组类型 getDateHeaders获取指定的头部信息,返回字日期数组类型 getIntHeaders获取指定的头部信息,返回字整数数组类型 getHeaderNames()获取客户端传递过来所有头部的名称 Http1.1协议中定义的头部信息如下:
通用头部信息(即请求和响应均可以使用的头部信息) general-header = Cache-Control
| Connection | Date | Pragma | Trailer | Transfer-Encoding | Upgrade | Via | Warning 除此之外还有请求专用的请求头:
request-header = Accept
| Accept-Charset | Accept-Encoding | Accept-Language | Authorization | Expect | From | Host | If-Match | If-Modified-Since | If-None-Match | If-Range | If-Unmodified-Since | Max-Forwards | Proxy-Authorization | Range
| Referer | TE | User-Agent
例如我们想判断用户浏览器的类型以及所使用的地区语言可以使用如下语句: package neusoft;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class RequestServlet2 */
@WebServlet("/r2")
public class RequestServlet2 extends HttpServlet { private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet() */
public RequestServlet2() { super();
// TODO Auto-generated constructor stub }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("</head>"); out.print("<body>"); out.print("您使用的浏览器信息为:" + request.getHeader("User-Agent")+ "<br>"); out.print("您使用的地区语言为" + request.getHeader("Accept-Language") + "<br>"); out.print("</body>"); out.print("</html>");
} /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response);// TODO Auto-generated method stub }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询