Question

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

Was it helpful?

Solution 3

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top