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

httpServletRequest httpServletResponse 使用说明

程序员文章站 2022-03-10 13:02:19
...

httpServletRequest httpServletResponse 使用说明

 

*************************************************************

httpServletRequest:客户端发送给服务端的请求,包含请求头与请求体

 

public interface HttpServletRequest extends ServletRequest {
    String BASIC_AUTH = "BASIC";
    String FORM_AUTH = "FORM";
    String CLIENT_CERT_AUTH = "CLIENT_CERT";
    String DIGEST_AUTH = "DIGEST";

    String getAuthType();          //获取请求认证类型

    Cookie[] getCookies();         //获得所有的cookie

    HttpSession getSession(boolean var1);
    HttpSession getSession();      //获得session

    String getHeader(String var1);
    int getIntHeader(String var1);
    long getDateHeader(String var1);  //根据参数获取对应的请求头

    Enumeration<String> getHeaders(String var1);
    Enumeration<String> getHeaderNames();
                                   //获得header

    String getMethod();           //获取请求类型,如Get、Post、Delete等

    String getRequestURI();       //获取请求路径名,如/hello-world/hello2
    StringBuffer getRequestURL(); //获取请求全路径名,如http://localhost:8080/hello-world/hello2


**************************
context-path:hello-world
servlet-path:hello-world2
输入路径:localhost:8080/hello-world/hello-world2/hello2?name=hello

    String getContextPath();      //获取contextPath,可在application.properties中
                                  通过server.servlet.context-path=/hello-world设置
                                  项目根路径为/hello-world

    String getServletPath();      //获取ServletPath,可在application.properties中通过
                                  spring.mvc.servlet.path=/hello-world2设置
                                  输出路径名:/hello-world2

    String getPathInfo();         //获取路径中除contextPath、ServletPath外的路径PathInfo,
                                  输出路径为:/hello2

    String getQueryString();     //获取请求路径中的查询参数,输出name=hello

    String getPathTranslated();  //获取项目实际路径,输出路径为:C:\Users\28401\AppData\Local\Temp\tomcat-docbase.9749315821574923200.8080\hello2


    void login(String var1, String var2) throws ServletException;
    void logout() throws ServletException;
                               //客户端登录、退出

    String getRequestedSessionId();            //获取请求的sessionId
    boolean isRequestedSessionIdValid();       //sessionId是否有效
    boolean isRequestedSessionIdFromCookie();  //sessionId是否存储在cookie中
    boolean isRequestedSessionIdFromURL();     //sessionId是否在url中

    String getRemoteUser();

    boolean isUserInRole(String var1);

    Principal getUserPrincipal();

    String changeSessionId();

    boolean authenticate(HttpServletResponse var1) throws IOException, ServletException;

    Part getPart(String var1) throws IOException, ServletException;
    Collection<Part> getParts() throws IOException, ServletException;

    <T extends HttpUpgradeHandler> T upgrade(Class<T> var1) throws IOException, ServletException;

    default PushBuilder newPushBuilder() {
        return null;
    }

    default Map<String, String> getTrailerFields() {
        return Collections.emptyMap();
    }

    default boolean isTrailerFieldsReady() {
        return false;
    }

    default HttpServletMapping getHttpServletMapping() {
        return new HttpServletMapping() {
            public String getMatchValue() {
                return "";
            }

            public String getPattern() {
                return "";
            }

            public String getServletName() {
                return "";
            }

            public MappingMatch getMappingMatch() {
                return null;
            }
        };
    }

}


****************************************

public interface ServletRequest {

    void setAttribute(String var1, Object var2);
    Object getAttribute(String var1);
    Enumeration<String> getAttributeNames();
    void removeAttribute(String var1);
                       //request域的属性操作

    String getParameter(String var1);
    Enumeration<String> getParameterNames();
    String[] getParameterValues(String var1);
    Map<String, String[]> getParameterMap();
                       //request查询请求参数

    ServletInputStream getInputStream() throws IOException;
    BufferedReader getReader() throws IOException;
                       //获取请求输入流

    String getCharacterEncoding();
    void setCharacterEncoding(String var1) throws UnsupportedEncodingException;
                      //request的字符编码

    ServletContext getServletContext();
                      //获得application应用上下文

    int getContentLength();      //获取远程文件大小,int形式返回
    long getContentLengthLong(); //获取远程文件大小,long形式

    String getContentType();     //获取请求类型

    String getProtocol();        //获取协议类型,如HTTP/1.1
    String getScheme();          //获得协议名称,如http

    String getServerName();  //获得主机名称,如localhost
    int getServerPort();     //获得应用服务端口,如8080

    String getLocalName();
    String getLocalAddr();
    int getLocalPort();     //本地端口

    String getRemoteAddr();
    String getRemoteHost();
    int getRemotePort();    //远程端口

    Locale getLocale();
    Enumeration<Locale> getLocales();

    boolean isSecure();

    AsyncContext startAsync() throws IllegalStateException;
    AsyncContext startAsync(ServletRequest var1, ServletResponse var2) throws IllegalStateException;

    boolean isAsyncStarted();
    boolean isAsyncSupported();

    AsyncContext getAsyncContext();

    RequestDispatcher getRequestDispatcher(String var1);
    DispatcherType getDispatcherType();

}

 

 

*********************************************************************

httpServletResponse:服务端返回的响应,包含请求头和请求体

 

public interface HttpServletResponse extends ServletResponse {
    int SC_CONTINUE = 100;
    int SC_SWITCHING_PROTOCOLS = 101;
    int SC_OK = 200;
    int SC_CREATED = 201;
    int SC_ACCEPTED = 202;
    int SC_NON_AUTHORITATIVE_INFORMATION = 203;
    int SC_NO_CONTENT = 204;
    int SC_RESET_CONTENT = 205;
    int SC_PARTIAL_CONTENT = 206;
    int SC_MULTIPLE_CHOICES = 300;
    int SC_MOVED_PERMANENTLY = 301;
    int SC_MOVED_TEMPORARILY = 302;
    int SC_FOUND = 302;
    int SC_SEE_OTHER = 303;
    int SC_NOT_MODIFIED = 304;
    int SC_USE_PROXY = 305;
    int SC_TEMPORARY_REDIRECT = 307;
    int SC_BAD_REQUEST = 400;
    int SC_UNAUTHORIZED = 401;
    int SC_PAYMENT_REQUIRED = 402;
    int SC_FORBIDDEN = 403;
    int SC_NOT_FOUND = 404;
    int SC_METHOD_NOT_ALLOWED = 405;
    int SC_NOT_ACCEPTABLE = 406;
    int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
    int SC_REQUEST_TIMEOUT = 408;
    int SC_CONFLICT = 409;
    int SC_GONE = 410;
    int SC_LENGTH_REQUIRED = 411;
    int SC_PRECONDITION_FAILED = 412;
    int SC_REQUEST_ENTITY_TOO_LARGE = 413;
    int SC_REQUEST_URI_TOO_LONG = 414;
    int SC_UNSUPPORTED_MEDIA_TYPE = 415;
    int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    int SC_EXPECTATION_FAILED = 417;
    int SC_INTERNAL_SERVER_ERROR = 500;
    int SC_NOT_IMPLEMENTED = 501;
    int SC_BAD_GATEWAY = 502;
    int SC_SERVICE_UNAVAILABLE = 503;
    int SC_GATEWAY_TIMEOUT = 504;
    int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

    void setStatus(int var1);   //设置响应状态码
    int getStatus();            //获得响应状态码

    void sendError(int var1, String var2) throws IOException;
    void sendError(int var1) throws IOException;
                                //设置响应码,并跳转到预先设置的页面

    void addCookie(Cookie var1);        //响应体中增加cookie

    boolean containsHeader(String var1);  //响应体中是否包含某header

    void setDateHeader(String var1, long var2);
    void addDateHeader(String var1, long var2);  //日期header

    void setHeader(String var1, String var2);
    void addHeader(String var1, String var2);   //添加header

    void setIntHeader(String var1, int var2);
    void addIntHeader(String var1, int var2);   //添加int类型的header

    String getHeader(String var1);
    Collection<String> getHeaders(String var1);
    Collection<String> getHeaderNames();        //获得header

    void sendRedirect(String var1) throws IOException;
                                               //设置跳转路径

    String encodeURL(String var1);          //重写url
    String encodeRedirectURL(String var1);  //重写跳转url


    default void setTrailerFields(Supplier<Map<String, String>> supplier) {
    }

    default Supplier<Map<String, String>> getTrailerFields() {
        return null;
    }
}


*******************************************************

public interface ServletResponse {
    String getCharacterEncoding();
                             //获取编码类型
    String getContentType(); //获取返回体类型

    ServletOutputStream getOutputStream() throws IOException;
    PrintWriter getWriter() throws IOException;
                            //获取输出流

    void setCharacterEncoding(String var1);
                            //设置编码类型

    void setContentLength(int var1);
    void setContentLengthLong(long var1);
                            //设置返回体的大小

    void setContentType(String var1); //设置返回体类型

    void setBufferSize(int var1);     //设置缓冲区大小
    int getBufferSize();              //获得缓冲区大小

    void flushBuffer() throws IOException;
                                      //刷新缓冲区
    void reset();
    void resetBuffer();               //清空缓冲

    boolean isCommitted();  //缓冲是否提交

    void setLocale(Locale var1);  //设置国际化类
    Locale getLocale();           //获得国际化类
}