返回为 null 的 Java Web 服务请求参数

标签 java http

我正在使用 Eclipse 开发 Web 服务,为了尝试它,我启动了 tomcat 服务器并尝试了一个带有参数的 http 请求。问题是我给出的参数似乎被忽略了:

enter image description here

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        if(session.getAttribute("lat") == null && session.getAttribute("lon") == null) {
            session.setAttribute("lat", request.getAttribute("lat"));
            session.setAttribute("lon", request.getAttribute("lon"));
            response.setContentType("text/plain");
            response.getWriter().append("RECEIVED");
        }
        else {

通过调试器,我可以看到对象 request 不包含我的参数。

http

最佳答案

您正在尝试获取 HttpSession 属性,而不是 URL 中传递的参数。你需要使用

request.getParameter("lat");

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

要了解 session 属性和请求参数之间的区别,请引用here

还可以获取Map中的所有参数

Map<String,String[]> getParameterMap()

Returns a java.util.Map of the parameters of this request.

Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

关于返回为 null 的 Java Web 服务请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58140480/

相关文章:

java - 我可以在正则表达式中组合 unicode 类别吗?

java - 如何使用 Jackson 将键值的 JSON 数组动态映射到子对象?

javascript - Uncaught Error : [$injector:unpr] Unknown provider: dependency1Provider <- dependency1 <- $http <- $compile

http - Crystal-lang 服务 index.html

java - Tomcat 连接超时是否会向 HTTP 客户端触发 500?

java - 您在复杂的构建过程中使用什么?

java - 我们可以将 java .class 转换为 .java 文件吗?那么 java 是如何被称为安全语言的呢?

java - 我无法理解以下代码

php - 使用 Post 和 httpUrlConnection 发送 url 参数

c# - HttpClient 究竟是如何处理超时的?