0

I want to pass a long series of request parameters (over 2000 characters in total) from one .jsp to another (via a URL), and make it seem to the receiving HTTPServletRequest as if it received the request parameters normally.

I cannot simply pass the URL normally as IE11 is truncating the URL at about 2000 characters (see What is the maximum length of a URL in different browsers?) so I need to have some kind of workaround.

It is trivial to save the url in the ClientSession with a key in one .jsp

public String addValue(String aString) {
    String key=""+UUID.randomUUID();
    mapValues.put(key, aString);
    return key;
}

and then retrieve it in the other .jsp

public String getValue(String key) {
        return mapValues.get(key);
}

However the other .jsp needs a HTTPServletRequest and not a string

I.e. I need to be able to do

public MyPosition(HttpServletRequest request) {
        this.id= (String)request.getParameter("ID");

Is there anyway of doing this by converting the retrieved url to a HTTPServletRequest?

I know that I could rewrite MyPosition to take a string and extract the data from there directly, but I would much rather not touch the very lengthy, legacy code.

If I could do setParameter on the request, then this would be a solution. But such an option is not available (see HttpServletRequest - SetParameter)

gordon613
  • 2,770
  • 12
  • 52
  • 81
  • Are you passing the values from *.jsp to another *.jsp? Or to a servlet? – Roshana Pitigala Jun 06 '18 at 14:39
  • From one .jsp to another .jsp – gordon613 Jun 06 '18 at 14:42
  • You want to pass the request which comes to the 1.jsp to 2.jsp by adding some more parameters.. right? – Roshana Pitigala Jun 06 '18 at 14:44
  • @RoshanaPitigala: that is definitely one possible direction of solving the problem, but it is not necessarily the only direction... Thanks! – gordon613 Jun 06 '18 at 14:52
  • what about storing the url somewhere and then giving that a unique identifier... then use the unique identifier to retrieve the url you assigned to it? (sending the unique identifier in the url instead of the whole url) – Jonathan Laliberte Jun 06 '18 at 15:13
  • @JonathanLaliberte that is exactly what I am doing and describe above! The question is what I do with the URL after I have retrieved it. It is a `string` and I need a `HTTPServletRequest`! – gordon613 Jun 06 '18 at 15:27
  • my bad, missed that part. I don't understand why you can't just save the url as a session attribute? Why do you need `HTTPServletRequest` ? – Jonathan Laliberte Jun 06 '18 at 15:30
  • @JonathanLaliberte. I am trying to solve the problem without adapting my old existing legacy code. If I have to, then I will! I attempted to explain this above in the penultimate paragraph... Thanks! – gordon613 Jun 07 '18 at 09:56

3 Answers3

1

The only way to modify an HttpServletRequest is to wrap it.

dimplex
  • 494
  • 5
  • 9
1

It sounds like you want to make a standard POST request instead of what sounds like a GET request.

Deadron
  • 5,135
  • 1
  • 16
  • 27
0

I tried both @dimplex's and @Deadron's solutions, which I think should both work, but didn't manage to implement either in the short time frame I had available.

I ended up replacing the HTTPServletRequest request parameter in the MyPosition function with String urlKey and adding the following line inside the function

RequestStr request=new RequestStr(cSession,urlKey);

now my existing code did not need to be changed at all, and request.getParameter("paramName") would call my function below.

public class RequestStr {
    String url = "";

    public RequestStr(ClientSession cSession, String urlKey) {
        super();
        this.url = cSession.getValue(urlKey);
    }

    public String getParameter(String aParam) {
        int i = url.indexOf("?" + aParam + "=");
        if (i == -1) {
            i = url.indexOf("&" + aParam + "=");
        }
        if (i == -1) {
            return null;
        } else {

            int j = url.indexOf("&", i + 1);
            if (j == -1) {
                return url.substring(i + aParam.length() + 2);
            } else {
                return url.substring(i + aParam.length() + 2, j);
            }
        }
    }

}

So all I needed to do was to save the very long URL in the session in a map with key urlKey and then in the request just passed this urlKey, and then I could retrieve the long URL via the urlKey and then decode it via my RequestStr class

gordon613
  • 2,770
  • 12
  • 52
  • 81
  • 1
    You need to make sure you call URLDecoder.decode on the query string params or you may have strings containing percent encoded values. Refer to https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection – Deadron Jun 27 '18 at 18:10