有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

http Java web服务请求参数返回为null

我正在使用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


共 (1) 个答案

  1. # 1 楼答案

    您正在尝试获取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.

    要了解会话属性和请求参数之间的区别,请参阅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.