javaweb之ServletRequest请求

系列文章目录
第一章 javaweb之Servlet接口
第二章 javaweb之ServletRequest请求


文章目录


原文链接 https://zhhll.icu/2021/javaweb/基础/2.请求/

请求

客户端请求由ServletRequest类型的request对象表示,在HTTP请求场景下,容器提供的请求对象的具体类型为HttpServletRequest

HTTP的请求消息分为三部分:请求行、请求头、请求正文。

请求行对应方法

// 获取请求行中的协议名和版本
public String getProtocol();

// 获取请求方式
public String getMethod();

// url中的额外路径信息
public String getPathInfo();

// url中的额外路径信息多对应的资源的真是路径
public String getPathTranslated();

// 获取请求URL所属的WEB应用程序路径,以/开头,表示整个web站点的根目录
public String getContextPath();

// 请求行中的参数
public String getQueryString();

// 获取请求行中的资源名,主机端口之后,参数之前的部分
public String getRequestURI();

// 获取Servlet所映射的路径
public String getServletPath();

网络连接信息相关方法

// 客户端的ip
public String getRemoteAddr();
//客户端的主机
public String getRemoteHost();
//客户端的端口
public int getRemotePort();
// 服务器接收当前请求的网络接口的ip对应的主机名
public String getLocalName();
// 服务器接收当前请求的网络接口的ip
public String getLocalAddr();
// 服务器接收当前请求的网络接口的端口
public int getLocalPort();
// 获取URL
public StringBuffer getRequestURL();
// 当前请求所指向的主机名
public String getServerName();
// 当前请求所连接的服务器端口号
public int getServerPort();
// 协议名
public String getScheme();

请求头信息

// 获取请求头
public long getDateHeader(String name);
public String getHeader(String name); 
public Enumeration<String> getHeaders(String name); 
public Enumeration<String> getHeaderNames();
public int getIntHeader(String name);
// 获取Content-Length头字段信息
public int getContentLength();
// 返回Content-Type头字段信息
public String getContentType();
// 返回请求消息的字符集编码,Content-Type头字段信息
public String getCharacterEncoding();


public String getAuthType();
    
public Cookie[] getCookies();

public String getRemoteUser();

public boolean isUserInRole(String role);

public java.security.Principal getUserPrincipal();

public String getRequestedSessionId();

public HttpSession getSession(boolean create);

public HttpSession getSession();

public String changeSessionId();

public boolean isRequestedSessionIdValid();

public boolean isRequestedSessionIdFromCookie();

public boolean isRequestedSessionIdFromURL();

public boolean isRequestedSessionIdFromUrl();

public boolean authenticate(HttpServletResponse response) 
  throws IOException,ServletException;

public void login(String username, String password) 
  throws ServletException;

public void logout() throws ServletException;

public Collection<Part> getParts() throws IOException, ServletException;

public Part getPart(String name) throws IOException, ServletException;

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

获取请求参数

// 读取url地址后的参数或者POST请求中application/x-www-form-urlencoded编码的实体
// 可以对编码内容自动完成参数的分解、提取以及解码
public String getParameter(String name);
public Enumeration<String> getParameterNames();
public String[] getParameterValues(String name);
public Map<String, String[]> getParameterMap();

// 获取流对象
public ServletInputStream getInputStream() throws IOException; 
public BufferedReader getReader() throws IOException;
posted @   拾光师  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示