gpt4 book ai didi

java - Spring/Java Web : how can I know a request is a form post?

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:33 24 4
gpt4 key购买 nike

我正在做一个 Spring 网站。该网站有许多访问者可以访问的页面。它还允许访问者填写并提交一些表格。

我想使用 Spring 拦截器(作为中心位置)来检查访问者的每个 Web 请求。如何判断网络请求是 get 请求还是表单 post 请求?

感谢和问候!

最佳答案

您可以按如下方式使用 getMethod() -

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("Request Intercepted for : "
+ request.getRequestURI());
System.out.println("Request type : "
+ request.getMethod());
return true;
}
}

表格documentation

java.lang.String getMethod()

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.

Returns: a String specifying the name of the method with which this request was made

关于java - Spring/Java Web : how can I know a request is a form post?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com