public class RequestAPIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("URI-->"+req.getRequestURI());//URI-->/tian/req
System.out.println("URL-->"+req.getRequestURL());//URL-->http://localhost:8080/tian/req
System.out.println("客户端ip地址--->"+req.getRemoteHost());//客户端ip地址--->0:0:0:0:0:0:0:1 相当于127.0.0.1
System.out.println("请求的方式"+req.getMethod());//请求的方式GET
}
}
[](()如何获取请求参数
req.getParameter("username"); //获取一个值的时候使用
req.getParameterValues("hobby") //获取多个值的时候使用
[](()代码演示
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/tian/param" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
兴趣爱好:<input type="checkbox" name="hobby" value="cpp">C++
<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="js">js<br>
<input type="submit">
</form>
</body>
</html>
xml配置
<servlet>
<servlet-name>ParamterServlet</servlet-name>
<servlet-class>com.tian.servlet.ParamterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ParamterServlet</servlet-name>
<url-pattern>/param</url-pattern>
</servlet-mapping>
servlet
package com.tian.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class ParamterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
String[] hobby=req.getParameterValues("hobby");