当前位置 : 主页 > 编程语言 > java >

09-HttpServletRequest类

来源:互联网 收集:自由互联 发布时间:2022-09-02
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 Syst

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");

上一篇:2021-11-26【Codeforces Round #756 (Div
下一篇:没有了
网友评论