后期参数变为空(随机)

发布于 2024-08-16 11:23:49 字数 2120 浏览 1 评论 0原文

我的 Web 应用程序是使用 Struts2 开发的,直到最近它都运行良好。突然,其中一个模块开始出现故障。

出现故障的模块是“更新学生详细信息”页面。该页面有很多字段,例如“学校名称”、“学位名称”等。

School 1: <input name="schoolName">

School 2: <input name="schoolName">

..... 
School n: <input name="schoolName">

如前所述,该页面直到最近都运行得很好。现在,“schoolName”、“ DegreeName”等的一个/多个值在服务器端以“”(空字符串)形式接收。

为了调试,我在 Eclipse 中使用了 firebug 和远程调试。我发现客户端的后置参数是正确的。例如,在其中一次提交期间,后置参数如下(我从 firebug 中注意到它们)。

Content-Type: multipart/form-data; boundary=---------------------------2921238217421 
Content-Length: 48893 

<OTHER_PARAMETERS> <!--Truncated for clarity --> 

-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

ABC Institute 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

Test School 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

XYZ 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

Texas Institute 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

XXXX School 

-----------------------------2921238217421-- 

但在服务器端,请求参数如下:

schoolName=[ABC Institute, Test School, XYZ, , XXXX School], 

在这种特殊情况下,“Texas Institute”被接收为“”(空字符串)。这种情况并没有持续发生。变为 NULL(或 EMPTY STRING)的参数对我来说似乎是随机的 - 在一个实例中,参数 schoolName[3] 变为 null,如上所示,参数 schoolName[2] 在另一次提交期间变为 null,等等。有时,没有一个参数无效。

以下是动作定义中的拦截器列表。

List of interceptors: 
----------------------
FileUploadInterceptor
org.apache.struts2.interceptor.FileUploadInterceptor
ServletConfigInterceptor
org.apache.struts2.interceptor.ServletConfigInterceptor  StaticParametersInterceptor
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor
ParametersInterceptor
com.opensymphony.xwork2.interceptor.ParametersInterceptor
MyCustomInterceptor
com.xxxx.yyyy.interceptors.GetLoggedOnUserInterceptor

这个问题对我来说显得相当奇怪,我无法将问题的确切原因归零。在这方面的任何帮助将不胜感激。提前致谢。

谢谢, 拉古拉姆

My web application is developed with Struts2 and it was working fine till recently. All of a sudden one of the modules has started malfunctioning.

The malfunctioning module is 'Update Student details' page. This page has a lot of fields like 'schoolName', 'degreeName', etc .

School 1: <input name="schoolName">

School 2: <input name="schoolName">

..... 
School n: <input name="schoolName">

As mentioned earlier, the page was working perfectly fine till recently. Now, one/many of the values of 'schoolName', 'degreeName', etc are being received as "" (EMPTY STRING) on the server-side.

For debugging, I used firebug and remote-debugging in eclipse. I find that the post-parameters are correct on the client-side. For instance, during one of the submissions the post-parameters were as below (i noted them from firebug).

Content-Type: multipart/form-data; boundary=---------------------------2921238217421 
Content-Length: 48893 

<OTHER_PARAMETERS> <!--Truncated for clarity --> 

-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

ABC Institute 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

Test School 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

XYZ 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

Texas Institute 
-----------------------------2921238217421 
Content-Disposition: form-data; name="schoolName" 

XXXX School 

-----------------------------2921238217421-- 

But on the server-side, the request params were as below:

schoolName=[ABC Institute, Test School, XYZ, , XXXX School], 

"Texas Institute" was received as "" (EMPTY STRING) in this particular case. This is not happening consistently. The parameters that become NULL (or EMPTY STRING) seem random to me - during one instance, parameter schoolName[3] became null as illustrated above, parameter schoolName[2] became null during yet another submission, etc. At times, none of the parameters are nullified.

The following is the list of the interceptors in the action definition.

List of interceptors: 
----------------------
FileUploadInterceptor
org.apache.struts2.interceptor.FileUploadInterceptor
ServletConfigInterceptor
org.apache.struts2.interceptor.ServletConfigInterceptor  StaticParametersInterceptor
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor
ParametersInterceptor
com.opensymphony.xwork2.interceptor.ParametersInterceptor
MyCustomInterceptor
com.xxxx.yyyy.interceptors.GetLoggedOnUserInterceptor

This issue appears rather weird to me and I have not been able to zero-in on the exact cause of the issue. Any help in this regard would be highly appreciated. Thanks in advance.

Thanks,
Raghuram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝情姑娘 2024-08-23 11:23:49

更新。

我的表单发送了一个多部分请求(因为表单上启用了文件上传,所以需要)。我决定记录到达服务器的后期数据。我修改了 RequestDumperFilter.java 以记录多部分发布数据。添加此过滤器后,参数丢失问题似乎已经停止(150 次提交的表单中 0 丢失)。我删除了过滤器并能够再次重现该问题。

谢谢,
拉古拉姆

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package filters;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.xml.ws.RequestWrapper;

import com.oreilly.servlet.multipart.BufferedServletInputStream;



/**
 * Example filter that dumps interesting state information about a request
 * to the associated servlet context log file, before allowing the servlet
 * to process the request in the usual way.  This can be installed as needed
 * to assist in debugging problems.
 *
 * @author Craig McClanahan
 * @version $Revision: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (dim., 28 janv. 2007) $
 */

public final class RequestDumperFilter implements Filter {


    // ----------------------------------------------------- Instance Variables


    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    private FilterConfig filterConfig = null;

    // --------------------------------------------------------- Public Methods


    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.filterConfig = null;

    }


    /**
     * Time the processing that is performed by all subsequent filters in the
     * current filter stack, including the ultimately invoked servlet.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {

        if (filterConfig == null)
        return;

    // Render the generic servlet request properties
    StringWriter sw = new StringWriter();
    PrintWriter writer = new PrintWriter(sw);
    writer.println("Request Received at " +
               (new Timestamp(System.currentTimeMillis())));
    writer.println(" characterEncoding=" + request.getCharacterEncoding());
    writer.println("     contentLength=" + request.getContentLength());
    writer.println("       contentType=" + request.getContentType());
    writer.println("            locale=" + request.getLocale());
    writer.print("           locales=");
    Enumeration locales = request.getLocales();
    boolean first = true;
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        if (first)
            first = false;
        else
            writer.print(", ");
        writer.print(locale.toString());
    }
    writer.println();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        writer.print("         parameter=" + name + "=");
        String values[] = request.getParameterValues(name);
        for (int i = 0; i < values.length; i++) {
            if (i > 0)
            writer.print(", ");
        writer.print(values[i]);
        }
        writer.println();
    }
    writer.println("          protocol=" + request.getProtocol());
    writer.println("        remoteAddr=" + request.getRemoteAddr());
    writer.println("        remoteHost=" + request.getRemoteHost());
    writer.println("            scheme=" + request.getScheme());
    writer.println("        serverName=" + request.getServerName());
    writer.println("        serverPort=" + request.getServerPort());
    writer.println("          isSecure=" + request.isSecure());

    // Render the HTTP servlet request properties
    if (request instanceof HttpServletRequest) {
        writer.println("---------------------------------------------");
        HttpServletRequest hrequest = (HttpServletRequest) request;
        writer.println("       contextPath=" + hrequest.getContextPath());
        Cookie cookies[] = hrequest.getCookies();
            if (cookies == null)
                cookies = new Cookie[0];
        for (int i = 0; i < cookies.length; i++) {
            writer.println("            cookie=" + cookies[i].getName() +
                   "=" + cookies[i].getValue());
        }
        names = hrequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
        String value = hrequest.getHeader(name);
            writer.println("            header=" + name + "=" + value);
        }
        writer.println("            method=" + hrequest.getMethod());
        writer.println("          pathInfo=" + hrequest.getPathInfo());
        writer.println("       queryString=" + hrequest.getQueryString());
        writer.println("        remoteUser=" + hrequest.getRemoteUser());
        writer.println("requestedSessionId=" +
               hrequest.getRequestedSessionId());
        writer.println("        requestURI=" + hrequest.getRequestURI());
        writer.println("       servletPath=" + hrequest.getServletPath());


        /*
        The following section is added by me to print the post data of a multipart request.
        */
        writer.println("=============================================");
        writer.println("POST-DATA:");
        writer.println("----------");
        String line;
        BufferedRequestWrapper bufferedRequest= new BufferedRequestWrapper(hrequest);
        //Here obtain InputStream to process POST data!
        InputStream is = bufferedRequest.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while((line = br.readLine()) != null){
              writer.println(line);
        }
        writer.println("=============================================");
        // Log the resulting string
        writer.flush();
        filterConfig.getServletContext().log(sw.getBuffer().toString());
        // Pass control on to the next filter
        chain.doFilter(bufferedRequest, response);

    }
    else
    {
        writer.println("=============================================");

        // Log the resulting string
        writer.flush();
        filterConfig.getServletContext().log(sw.getBuffer().toString());

        // Pass control on to the next filter
            chain.doFilter(request, response);
    }
    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;

    }


    /**
     * Return a String representation of this object.
     */
    public String toString() {

    if (filterConfig == null)
        return ("RequestDumperFilter()");
    StringBuffer sb = new StringBuffer("RequestDumperFilter(");
    sb.append(filterConfig);
    sb.append(")");
    return (sb.toString());

    }

    public class BufferedRequestWrapper extends HttpServletRequestWrapper {

        ByteArrayInputStream bais;
        ByteArrayOutputStream baos;
        BufferedServletInputStream bsis;
        byte [] buffer; 

        public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
        super(req);
        // Read InputStream and store its content in a buffer.
        InputStream is = req.getInputStream();
        baos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int letti;
        while ((letti=is.read(buf))>0) baos.write(buf,0,letti);
        buffer = baos.toByteArray();
        }

        public ServletInputStream getInputStream() {
        try {
        // Generate a new InputStream by stored buffer
        bais = new ByteArrayInputStream(buffer);
        // Istantiate a subclass of ServletInputStream
        // (Only ServletInputStream or subclasses of it are accepted by the servlet engine!)
        bsis = new BufferedServletInputStream(bais); 
        } 
        catch (Exception ex) {
        ex.printStackTrace();
        } 
        finally {
        return bsis;
        }
        }

        }

    /*
    Subclass of ServletInputStream needed by the servlet engine.
    All inputStream methods are wrapped and are delegated to 
    the ByteArrayInputStream (obtained as constructor parameter)!
    */
    public class BufferedServletInputStream extends ServletInputStream {

    ByteArrayInputStream bais;

    public BufferedServletInputStream(ByteArrayInputStream bais) {
    this.bais = bais;
    }

    public int available() {
    return bais.available();
    }

    public int read() {
    return bais.read();
    }

    public int read(byte[] buf,int off,int len) {
    return bais.read(buf,off,len);
    }

    }


}

An update.

My form sends a multipart request (needed since file upload is enabled on the form). I decided to log the post-data that comes to the server. I modified RequestDumperFilter.java to log the multipart post data. After i added this filter, the parameter loss issue seems to have ceased (0 loss out of 150 submissions of form). I removed the filter and was able to reproduce the issue again.

Thanks,
Raghuram

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package filters;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.xml.ws.RequestWrapper;

import com.oreilly.servlet.multipart.BufferedServletInputStream;



/**
 * Example filter that dumps interesting state information about a request
 * to the associated servlet context log file, before allowing the servlet
 * to process the request in the usual way.  This can be installed as needed
 * to assist in debugging problems.
 *
 * @author Craig McClanahan
 * @version $Revision: 500674 $ $Date: 2007-01-28 00:15:00 +0100 (dim., 28 janv. 2007) $
 */

public final class RequestDumperFilter implements Filter {


    // ----------------------------------------------------- Instance Variables


    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    private FilterConfig filterConfig = null;

    // --------------------------------------------------------- Public Methods


    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.filterConfig = null;

    }


    /**
     * Time the processing that is performed by all subsequent filters in the
     * current filter stack, including the ultimately invoked servlet.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {

        if (filterConfig == null)
        return;

    // Render the generic servlet request properties
    StringWriter sw = new StringWriter();
    PrintWriter writer = new PrintWriter(sw);
    writer.println("Request Received at " +
               (new Timestamp(System.currentTimeMillis())));
    writer.println(" characterEncoding=" + request.getCharacterEncoding());
    writer.println("     contentLength=" + request.getContentLength());
    writer.println("       contentType=" + request.getContentType());
    writer.println("            locale=" + request.getLocale());
    writer.print("           locales=");
    Enumeration locales = request.getLocales();
    boolean first = true;
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        if (first)
            first = false;
        else
            writer.print(", ");
        writer.print(locale.toString());
    }
    writer.println();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        writer.print("         parameter=" + name + "=");
        String values[] = request.getParameterValues(name);
        for (int i = 0; i < values.length; i++) {
            if (i > 0)
            writer.print(", ");
        writer.print(values[i]);
        }
        writer.println();
    }
    writer.println("          protocol=" + request.getProtocol());
    writer.println("        remoteAddr=" + request.getRemoteAddr());
    writer.println("        remoteHost=" + request.getRemoteHost());
    writer.println("            scheme=" + request.getScheme());
    writer.println("        serverName=" + request.getServerName());
    writer.println("        serverPort=" + request.getServerPort());
    writer.println("          isSecure=" + request.isSecure());

    // Render the HTTP servlet request properties
    if (request instanceof HttpServletRequest) {
        writer.println("---------------------------------------------");
        HttpServletRequest hrequest = (HttpServletRequest) request;
        writer.println("       contextPath=" + hrequest.getContextPath());
        Cookie cookies[] = hrequest.getCookies();
            if (cookies == null)
                cookies = new Cookie[0];
        for (int i = 0; i < cookies.length; i++) {
            writer.println("            cookie=" + cookies[i].getName() +
                   "=" + cookies[i].getValue());
        }
        names = hrequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
        String value = hrequest.getHeader(name);
            writer.println("            header=" + name + "=" + value);
        }
        writer.println("            method=" + hrequest.getMethod());
        writer.println("          pathInfo=" + hrequest.getPathInfo());
        writer.println("       queryString=" + hrequest.getQueryString());
        writer.println("        remoteUser=" + hrequest.getRemoteUser());
        writer.println("requestedSessionId=" +
               hrequest.getRequestedSessionId());
        writer.println("        requestURI=" + hrequest.getRequestURI());
        writer.println("       servletPath=" + hrequest.getServletPath());


        /*
        The following section is added by me to print the post data of a multipart request.
        */
        writer.println("=============================================");
        writer.println("POST-DATA:");
        writer.println("----------");
        String line;
        BufferedRequestWrapper bufferedRequest= new BufferedRequestWrapper(hrequest);
        //Here obtain InputStream to process POST data!
        InputStream is = bufferedRequest.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while((line = br.readLine()) != null){
              writer.println(line);
        }
        writer.println("=============================================");
        // Log the resulting string
        writer.flush();
        filterConfig.getServletContext().log(sw.getBuffer().toString());
        // Pass control on to the next filter
        chain.doFilter(bufferedRequest, response);

    }
    else
    {
        writer.println("=============================================");

        // Log the resulting string
        writer.flush();
        filterConfig.getServletContext().log(sw.getBuffer().toString());

        // Pass control on to the next filter
            chain.doFilter(request, response);
    }
    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;

    }


    /**
     * Return a String representation of this object.
     */
    public String toString() {

    if (filterConfig == null)
        return ("RequestDumperFilter()");
    StringBuffer sb = new StringBuffer("RequestDumperFilter(");
    sb.append(filterConfig);
    sb.append(")");
    return (sb.toString());

    }

    public class BufferedRequestWrapper extends HttpServletRequestWrapper {

        ByteArrayInputStream bais;
        ByteArrayOutputStream baos;
        BufferedServletInputStream bsis;
        byte [] buffer; 

        public BufferedRequestWrapper(HttpServletRequest req) throws IOException {
        super(req);
        // Read InputStream and store its content in a buffer.
        InputStream is = req.getInputStream();
        baos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int letti;
        while ((letti=is.read(buf))>0) baos.write(buf,0,letti);
        buffer = baos.toByteArray();
        }

        public ServletInputStream getInputStream() {
        try {
        // Generate a new InputStream by stored buffer
        bais = new ByteArrayInputStream(buffer);
        // Istantiate a subclass of ServletInputStream
        // (Only ServletInputStream or subclasses of it are accepted by the servlet engine!)
        bsis = new BufferedServletInputStream(bais); 
        } 
        catch (Exception ex) {
        ex.printStackTrace();
        } 
        finally {
        return bsis;
        }
        }

        }

    /*
    Subclass of ServletInputStream needed by the servlet engine.
    All inputStream methods are wrapped and are delegated to 
    the ByteArrayInputStream (obtained as constructor parameter)!
    */
    public class BufferedServletInputStream extends ServletInputStream {

    ByteArrayInputStream bais;

    public BufferedServletInputStream(ByteArrayInputStream bais) {
    this.bais = bais;
    }

    public int available() {
    return bais.available();
    }

    public int read() {
    return bais.read();
    }

    public int read(byte[] buf,int off,int len) {
    return bais.read(buf,off,len);
    }

    }


}
蓝眼睛不忧郁 2024-08-23 11:23:49

这个 tomcat 错误怎么样: https://issues.apache.org/bugzilla/ show_bug.cgi?id=37794

当 HTTP/1.1 客户端使用传输编码 POST 时:分块到 servlet,
getParameter() 系列和 getQueryString() 方法无法检索
不返回任何内容即可得到正确结果。相反,读取直到文件结尾
在 servlet 输入流上生成正确的结果。

6.0.21 中已修复。

What about this tomcat bug: https://issues.apache.org/bugzilla/show_bug.cgi?id=37794

When an HTTP/1.1 client POST with transfer-encoding: chunked to a servlet,
the getParameter() family and getQueryString() methods fail to retrieve the
correct result by returning nothing. In contrast, reading until end-of-file
on the servlets input stream produces correct result.

The fix is in 6.0.21.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文