Pergunta

I am building a JSON string in javascript and posting the JSON to a jsp page via an Ajax POST

Here is my scriptlet where I can retrieve the post data and access the JSON object.

<% 
    JSONObject responseObject = new JSONObject();
    try {
            BufferedReader in = new BufferedReader(new InputStreamReader( request.getInputStream()));
            String postStream = in.readLine();
            in.close();
            JSONObject requestObject = new JSONObject(postStream);
            JSONArray qJSONArray = requestObject.getJSONArray("questions");
        } catch (JSONException e) {
            out.print(responseObject.toString());
        }
%>

I would like to be able to do the same thing using only Expression Language. What is the best way to do it? And can it be done using the standard jstl libraries core,fmt,functions? Or will I need to access any other libraries?

If I do need another library is there one you can access via a public url in the same way the jstl libraries are accessed ?

e.g.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Foi útil?

Solução

Generally what I've seen people do with EL, is do the real work in a servlet, save the output to a request object that they then forward on to a JSP that merely reads the output from the request object and prints it onto the page. But with AJAX, that would be overkill. No need for the servlet to forward on to a JSP, because there's no need for styling (especially with Json). So, make the process in the back-end a servlet rather than JSP, and you can keep the Java code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top