ServletRequest获取请求信息

四个重要的方法:

String getParameter(java.lang.String name):将请求参数的值作为字符串返回,如果参数不存在,则返回null。
Map 《java.lang.String,java.lang.String[]> getParameterMap() 返回一个java.util.Map这个请求的参数的映射
Enumeration《java.lang.String> getParameterNames() :返回包含该请求中包含的参数名称的字符串对象的枚举。
String[] getParameterValues(java.lang.String name) :返回包含给定请求参数的所有值的字符串对象数组,如果参数不存在,则返回null。

代码演示:
首先建一个HTML文件,填写表单信息
这里的action要与web.xml文件配置的要相同

  <servlet>
      <servlet-name>ServletRequest01servlet-name>
      <servlet-class>com.alin.ServletRequest01servlet-class>
  servlet>
  <servlet-mapping>
      <servlet-name>ServletRequest01servlet-name>
      <url-pattern>/servletRequest01url-pattern>
  servlet-mapping>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
    <form action="servletRequest01" method="post">
        username: <input type="text" name="username" />
        password: <input type="text" name="password" />
        <br><br>
        interests:
        <input type="checkbox" name="interests" value="football"/>football
        <input type="checkbox" name="interests" value="basketball"/>basketball
        <input type="checkbox" name="interests" value="run"/>run
        <input type="checkbox" name="interests" value="readBooks"/>readBooks
        <input type="checkbox" name="interests" value="watchTV"/>watchTV
        <br>
        <input  type="submit" value="Submit"/>
    form>
body>
html>

新建一个ServletRequest01类

package com.alin;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletRequest01 implements Servlet {

    @Override
    public void destroy() {
    }
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public void init(ServletConfig arg0) throws ServletException {
    }
    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        System.out.println("开始了");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:" + username + "--> password:" + password + "\n");

        String interests = request.getParameter("interests");
        System.out.println("interests:" + interests + "\n");

        Enumeration names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = request.getParameter(name);
            System.out.println("name:" + name + "--> value:" + value);
        }
        System.out.println();

        String[] interesting = request.getParameterValues("interests");
        for(String interest : interesting) {
            System.out.println("interests:"+interest);
        }
        System.out.println();

        Map map = request.getParameterMap();
        for(Entry entry : map.entrySet()) {
            System.out.println(entry.getKey() +":"+ Arrays.asList(entry.getValue()));
        }
    }

}

运行Tomcat服务器
ServletRequest获取请求信息_第1张图片
运行结果如下:.
ServletRequest获取请求信息_第2张图片

request可以获取请求的所有信息,RequestServlet的子接口HttpRequestServlet下也由很多的方式

但是前提是先要将 ServletRequest对象强转成HttpRequestServlet。

HttpServletRequest httpServletRequest = (HttpServletRequest) request;

比如:

 HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    //将该请求的URL从协议名称返回到HTTP请求的第一行中的查询字符串。
    String requestURI = httpServletRequest.getRequestURI();
    System.out.println(requestURI);
    //返回使用此请求的HTTP方法的名称,例如GET、POST或PUT
    String method = httpServletRequest.getMethod();
    System.out.println(method);
    //返回路径之后的请求URL中包含的查询字符串
    String queryString = httpServletRequest.getQueryString();
    System.out.println(queryString);

    String servletPath = httpServletRequest.getServletPath();
    System.out.println(servletPath);
    System.out.println(httpServletRequest.getServerPort());
    System.out.println(httpServletRequest.getSession());

结果如下
ServletRequest获取请求信息_第3张图片

你可能感兴趣的:(java)