views:

9

answers:

1

Hi,

I'm saving an image as a blob using the following, but I'm not sure how to carry a message through the final redirect to display to the user:

JSP file:

<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String action = blobstoreService.createUploadUrl("/servletimg");
%>

<form method="POST" action="<%= action %>" enctype="multipart/form-data">
   ...
</form>

The target servlet:

public class ServletImg extends HttpServlet 
{   
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    {
         saveImg(...);

         req.setAttribute("msg", "worked ok!");

         resp.sendRedirect("/");    
    }
}

The final jsp page we get redirected back to:

if (request.getAttribute("msg") == null) {
    Log.e("Hey we're missing the expected attribute!!!");
}

It all works ok, my image gets saved etc, but I don't see the "msg" attribute when redirected back to the main jsp page. Is there a way to carry a message through, or do I have to append it as parameters in the redirect, like:

resp.sendRedirect("/?msg=it worked ok!");

Thanks

A: 

A redirect basically instructs the client to fire a new HTTP request to the server. The initial request (and response) will be garbaged, including all of the attributes set. So yes, you really need to pass a parameter along the redirect URL.

response.sendRedirect("index.jsp?msg=" + URLEncoder.encode("worked ok!", "UTF-8"));

and then in JSP

<p>Message: ${param.msg}</p>

Alternatively, you can instead also just forward to the resource in question, i.e.

request.setAttribute("msg", "worked ok!");
request.getRequestDispatcher("/index.jsp").forward(request, response);

and then in JSP (as a shorthand for the ugly and discouraged scriptlet with request.getAttribute("msg")):

<p>Message: ${msg}</p>

With a forward, the initial request will still be available in the targeted resource.

BalusC
Ah ok, yeah if I use the forward() method, then app engine complains about it: [HTTP ERROR 500 - Problem accessing /index.jsp. Reason: Expected a redirect, tried to write content instead.] I guess this is a requirement specific to the blobstore service. I can use the parameters in the url instead then. Thanks.
The URL was just an example. You need to use the webcontent-relative path of the *actual* page which is been displayed as if you're requesting `/` by webbrowser. I have no idea what page it is, so I just used an example. It can for instance be `/WEB-INF/index.jsp` or `/home.jsp` or whatever. It's at least the page which should display the message :)
BalusC
Oh no, I mean if I use the forward() method, with a working url, app engine throws that HTTP 500 exception. I'm sure the url is valid. So I think I can't use forward(), but must use redirect().
Hmm, that must be another GAE restriction then. Sorry, I didn't knew that, I don't use GAE. Go ahead with the redirect along with a parameter then. URL encoding is necessary since for example the space is an invalid character in URL's. As an another alternative you can also store the message in session (`session.setAttribute("msg", "it works!")`) and access it the same way (`${msg}`), but this has impact when the enduser has multiple windows open in the same session.
BalusC
Cool sounds good, thanks for your help again.
You're welcome.
BalusC