简体   繁体   中英

How do I send a list to the jsp that requested it?

Let us suppose there is a jsp that needs to display the list of files shared shared by a particular IP . Client opens that jsp on his local server and the request is sent to a remote server to fetch the list.

Servlet on the server processes the request and fetches the list of files shared by that IP. Now how do the servlet send that list to the jsp page that requested it ?

JSP :

connection.openConnection(); // Connection sends IP as the query parameter to the 
                             // remote servlet

Servlet :

doGet(..parameters..) {
 list = getList(forThatIP);
 // NOW HOW DO I SEND THIS LIST ?
}

One method that I thought was to send the whole data as a query string to a servlet on the client side(running a server such as tomcat) and then stash that list onto some file.Later jsp can parse the file and display the data. But that doesn't seem to be a good option.

Note: JSP is invoked when a servlet forwards after successfully sending the IP to remote servlet

You can use request.setAttribute() in the Servlet. Then you can use a JSP tag to retrieve the value in JSP. Investigate a bit on that.

EDIT :

In the Servlet doGet method, you can set the an attribute, say listOfFiles as:

resquest.setAttribute("listOfFiles",list);

Then in the JSP you can retirieve it using the EL expression:

${listOfFiles}

which is an inbuilt feature of JSP.

Alternatively, you can access it using

<% request.getAttribute("listOfFiles")%>

but it is bad to embed Java scriptlets inside JSPs.

If you're passing complex data (lists etc.) over a connection, you can look into using JSON or XML. Your JSP code should be able to parse either easily with the right library.

Actually it depends on how the jsp is invoked.

Do you perform a post onto it ? Does the servlet perform a forward ? Or is a sendRedirect() ?

According to this there are many ways to send data to your jsp. One is using request attributes or better, using request scoped beans. One other can be posting some sort of rappresentation of your list as a post parameter (be it json, xml or some custom format of yours).

Another thing to consider is, what are you using ? JSF ? Some Spring library ? According to that there may be other better (or worse) ways to send datas.

If working with simple JSP/Servlet on a small project I'd personally go with the request.setAttribute() way, indeed this will force me to invoke the jsp via something like

request.setAttribute("myList", yourListObject);
request.getRequestDispatcher("yourjsp.jsp").forward(request, response);

then in the jsp you can:

<% Object myListObj = pageContext.getRequest().getAttribute("myList"); %>

When you call a servlet from code, the best way is to use an HttpServlet and mimic the browser behavior.

Passing the data in the URL is a GET request. Useful only with relatively small chunks of data, but the semantics look like you want ("get a bit of data"). Something like http://myremoteserver.com/myServlet?ip=... . You will need a proper encoding of the parameters (the Java API can handle that).

Passing the data by writting into the stream will be a POST request. It has no limit in the data passed to the server, but the semantics is different ("change something in the system"). You could pass your data as a parameter inside the contents written, I am not really sure how to decode that. Another alternative is using a Servlet (no HttpServlet ) and just treat the raw data.

In both cases the response will be returned in the connection output stream. You can use whatever format you like (an standard one like JSON will probably be best, even that defining your own XML). In this case, a viable alternative would be filenames separated by a 'safe' character (like \\n or | ). Quick, but it will be less flexible in the future.

I would go for a GET request and JSON encoding .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM