Servlet 使用介绍(2)

xiaoxiao2021-02-28  95

说明

本篇由于介绍Servlet容器回传请求方法service(ServletRequest req, ServletResponse res);传入参数用户请求参数request和请求返回参数response的介绍。

request和response

Servlet-api.jar 中定义了两个基本的接口用于处理用户请求处理(ServletRequest)和用户响应返回(ServletResponse)。同时,对应的javax.servlet.http中对其进行扩展,针对该接口重定义了两个对应的接口:HttpServletRequest和HttpServletResponse,这里主要通过源码介绍其基本的用法,后面会通过示例对其进行扩展演示使用方法。

Request

用户请求,即用户对服务端发起的用户请求数据,servlet-api.jar中提供了两个接口:一个通用原始的ServletRequest,一个针对http协议的接口HttpServletRequest(继承自ServletRequest). 如下,为ServletRequest的源码:

package javax.servlet; import java.io.*; import java.util.*; public interface ServletRequest { /** *通过参数名获取value,若参数名不存在,返回null. * 获取的attribute可能是servlet设置的客户端信息,也可以是 *通过setAttribute设置的attribute. * */ public Object getAttribute(String name); /** *attribute的所有属性名集合. */ public Enumeration<String> getAttributeNames(); /** *返回正文中的字串编码 */ public String getCharacterEncoding(); public void setCharacterEncoding(String env) throws UnsupportedEncodingException; /** *返回正文的长度,若请求失败(未知),返回-1. */ public int getContentLength(); /** *获取正文MIME类型,获取失败返回null. */ public String getContentType(); /** *获取输入流,读取请求内容,ServletInputStream继承了InputStream方法,添加按行读取 *方法 */ public ServletInputStream getInputStream() throws IOException; /** *返回请求的参数通过给定的name(单个数值时),返回String或者null. *按顺序匹配,匹配首个name. */ public String getParameter(String name); /** *返回Enumeration<String>对象,空的或者多个String集合. */ public Enumeration<String> getParameterNames(); public String[] getParameterValues(String name); public Map<String, String[]> getParameterMap(); /** *返回请求协议 */ public String getProtocol(); /** *请求scheme. */ public String getScheme(); /** *获取服务器的host名 */ public String getServerName(); /** *请求服务端监听端口 */ public int getServerPort(); /** *获取一个BufferedReader输入流 */ public BufferedReader getReader() throws IOException; /** *返回客户端的ip地址 */ public String getRemoteAddr(); /** *返回客户端的主机名 */ public String getRemoteHost(); public void setAttribute(String name, Object o); public void removeAttribute(String name); /** *返回客户端的Locale环境 */ public Locale getLocale(); /** *默认返回Local的Enumeration. */ public Enumeration<Locale> getLocales(); /** *是否做了安全策略(HTTPS) */ public boolean isSecure(); /** *获取一个RequestDispatcher对象,可用来做跳转使用. */ public RequestDispatcher getRequestDispatcher(String path); /** * @deprecated As of Version 2.1 of the Java Servlet API, * use {@link ServletContext#getRealPath} instead. */ public String getRealPath(String path); /** * * @since Servlet 2.4 */ public int getRemotePort(); /** * * @since Servlet 2.4 */ public String getLocalName(); /** * * @since Servlet 2.4 * */ public String getLocalAddr(); /** * * @since Servlet 2.4 */ public int getLocalPort(); /** * * @since Servlet 3.0 */ public ServletContext getServletContext(); /** * * @since Servlet 3.0 */ public AsyncContext startAsync() throws IllegalStateException; /** * * @since Servlet 3.0 */ public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException; /** * * @since Servlet 3.0 */ public boolean isAsyncStarted(); /** * * @since Servlet 3.0 */ public boolean isAsyncSupported(); /** * * @since Servlet 3.0 */ public AsyncContext getAsyncContext(); /** * * @since Servlet 3.0 */ public DispatcherType getDispatcherType(); }

如上,为ServletRequest的源码,主要是获取请求参数和输入流。

Response

用户请求响应,响应返回用户请求数据,和Request类似,主要是获取客户端和服务端基本信息以及获取一个输出流用于返回响应数据。源码如下:

public interface ServletResponse { public String getCharacterEncoding(); /** * @since Servlet 2.4 */ public String getContentType(); /** *获取输出流ServletOutputStream */ public ServletOutputStream getOutputStream() throws IOException; /** *获取输出流PrintWriter */ public PrintWriter getWriter() throws IOException; /** * * @since Servlet 2.4 * */ public void setCharacterEncoding(String charset); public void setContentLength(int len); public void setContentType(String type); public void setBufferSize(int size); public int getBufferSize(); public void flushBuffer() throws IOException; /** * * @since Servlet 2.3 */ public void resetBuffer(); public boolean isCommitted(); public void reset(); public void setLocale(Locale loc); public Locale getLocale(); }

返回较为简单,主要是通过输出流,将需要返回的数据写入即可。servlet目前貌似只支持htpp协议,Tomcat的对应连接器配置如下:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

Tomcat会对于请求的数据格式进行验证,若非http格式数据会禁止访问,返回404。因此一般对于servlet的创建需要继承HttpServlet来处理。

Enjoytoday,EnjoyCoding

转载请注明原文地址: https://www.6miu.com/read-47128.html

最新回复(0)