0

In trying to get parameters from the request in a JSP, and for some reason we can't use servlets.

I want to emulate that servlet behavior (doPost, doGet, doPut, etc) in a JSP page, we have a lot of them. Something like:

JSP File:

    <%
        new SomeClass(request, response).doRequest();
    %>
    < !--HTML-->

I know i can get the request method with request.getMethod(), the GET parameters parsing request.getQueryString(), and the body parsing the data in request.getReader().

But the body looks like this when submitting from a form



------WebKitFormBoundaryklIJzgCDUQ00bFB8
Content-Disposition: form-data; name="a"

123
------WebKitFormBoundaryklIJzgCDUQ00bFB8
Content-Disposition: form-data; name="b"

456
------WebKitFormBoundaryklIJzgCDUQ00bFB8
Content-Disposition: form-data; name="c"; filename="text.txt"
Content-Type: text/plain

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sed erat justo. Vestibulum id dui felis. Donec sagittis, nibh sit amet interdum dignissim, magna velit pulvinar odio, ut ornare dui nunc et quam. Sed vel lorem vestibulum, semper libero a, aliquet nunc. Praesent urna tortor, euismod nec dolor hendrerit, scelerisque vehicula nisl. Nunc a interdum mi, sed rhoncus elit. Sed nec lacus ultrices, dictum tellus sed, aliquam arcu. In pretium imperdiet dui, et rutrum ipsum commodo sit amet. Aliquam cursus metus ac imperdiet aliquam.
------WebKitFormBoundaryklIJzgCDUQ00bFB8--

So it won't be easy to parse.

Then in that class i'll have both the GET parameters and body parameters separately, the request method and files maybe.

Is there an utility, class, framework that can do this parse from the request object, or any advice you can give me.

I'm not against using servlets, but we have a lot of JSP files already created, and we will have to specify a servlet path for the each one, like /posts

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dpaladin
  • 67
  • 1
  • 11

3 Answers3

0

request.getParameter("a") would do for both GET and POST. In EL (Expression language): ${param.a} placeable in the HTML.

File uplpad depends a bit on how it is realised (different possibilities). You might inspect the WEB-INF/web.xml.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You should be using request.getParameter (See javadoc) and/or getParameterNames, getParameterValues, getParameterMap to access both query string (GET) and form body (POST) parameters.

Parsing the query string and/or form body is almost never the right thing to do.

There's nothing about JSP that should prevent you from handling this in the same way a servlet would.

Tim
  • 6,406
  • 22
  • 34
  • Yeah, the problem with that is that i don't know if parameter 'a' came from GET or POST, and that is what i want. I want something like in PHP, you have $_GET, $_POST, $_FILES, separately. And `getParameterNames` doesn't work when posting with `multipart/form-data` – dpaladin May 08 '14 at 15:06
  • Your terminology is a little unclear (probably because PHP uses incorrect terminology here). Do you care about whether the parameter came on the query string vs the request body, or do you care about whether the request was a GET or POST? A GET request will only have query string parameters (because it has no body) but a POST request can have both query string and request body (form) parameters. – Tim May 12 '14 at 06:05
  • Yes, i want both things. Sorry if it seems ridiculous, i did it parsing the query string and request body manually depending on the requestType. I'll post it in a moment. – dpaladin May 12 '14 at 15:01
0

I ended up parsing the request manually depending on the RequestType:

String contentType = getContentType();
if(contentType == null){
    reader = request.getReader();
} else if (contentType.startsWith("multipart/")) {
    getMultipartParameters();
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
    getNameValueParameters();
} else {
    reader = request.getReader();
}

If the type is something else, i get a reference to the request reader for parsing later.

If it's multipart, i parse it with Apache Commons FileUpload. If it's form-urlencoded, i parse a name-value string from the body.

private void getMultipartParameters(){
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            for (FileItem fi : upload.parseRequest(request)) {
                if(fi.isFormField()){
                    Funciones.putInArrayMap(POST, fi.getFieldName(), fi.getString());
                } else {
                    Funciones.putInArrayMap(FILES, fi.getFieldName(), fi);
                }
            }
        } catch(FileUploadException e){
            Log.write(e);
        }
}

_

private void getNameValueParameters(){
    try {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = request.getReader();
        String line = br.readLine();
        while(line != null){
            sb.append(line);
            line = br.readLine();
        }
        POST = Funciones.getQueryMap(sb.toString(), getCharset());
    } catch (IOException ex) {
        Log.write(ex);
    }
}

_

private void getParameters(){
    String qs = request.getQueryString();
    GET = Funciones.getQueryMap(qs, getCharset());
}

In all cases, i parse the query string using the same name-value code.

All of that gets saved in these variables:

protected Map<String, List<String>> GET = new HashMap<>();
protected Map<String, List<String>> POST = new HashMap<>();
protected Map<String, List<FileItem>> FILES = new HashMap<>();

I'm still open to better ways of doing this.

dpaladin
  • 67
  • 1
  • 11