tags:

views:

21

answers:

1

Hi,

I have a servlet forwarding a request to another url in the same domain. I want to pass a string between the two, like:

// ServletVerifyUser.java:
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    // do some work..

    req.setAttribute("message", "Thanks for signing up!");

    req.getRequestDispatcher("/login.jsp").forward(req, resp); 
}

// login.jsp
<%
    // message is null.
    String message = request.getAttribute("message");

    <p><%= message %></p>
%>

Does the request object not work like this?

In my case, I am running ServletVerifyUser when a user clicks a url I generate for them after signup. At the end, I'd like to redirect them to my login page, but embed the message in the request. Then the login page can just render whatever the verify servlet prepared for it,

Thanks

------------------ Edit -------------------------

Ok so looks like the problem was that my login page is actually located at:

/login/index.jsp

on my local test machine, I was using this string for the url:

"/login"

(I tried to make the above example simpler by just showing "/login.jsp" because I didn't think this would make a difference, the url was still being resolved ok in the browser).

But when I use: "/login", the attributes are gone, if I use the full path: "/login/index.jsp", then the attributes are present.

I put the login jsp in a separate folder like this in order to keep nicer look urls, for example I can just give a user:

www.mysite.com/login

instead of:

www.mysite.com/login.jsp

but there is probably a better way to do this too (still really new with this)

Thank you

A: 

First, you'd normally use EL (Expression Language) to display it:

<p>${message}</p>

Second, the request attribute may be null in the view if you're actually doing a redirect to the view using response.sendRedirect("/login.jsp"). Even though the code displays you're doing a forward, but you told that you were redirecting to the login page. A redirect basically instructs the client to create a new request and hereby the original request including all of its attributes will be garbaged.

See also:


Update as per your update, the /login/index.jsp is in turn apparently doing a redirect. I'd suggest to get rid of it and just have a /login.jsp. You can eventually hide it in /WEB-INF/login.jsp so that you can keep using nice URL's. Rewrite LoginServlet (of which I assume that it's listening on an url-pattern of /login) as follows:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userDAO.find(username, password);
    if (user != null) {
        request.getSession().setAttribute("user", user);
        response.sendRedirect("home");
    } else {
        request.setAttribute("message", "Unknown user, please try again");
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
    }
}

Also see the 2nd link which I posted in the See also list here above.

BalusC
Yeah I am definitely using [req.getRequestDispatcher("/login.jsp").forward(req, resp);], and not the redirect method - I just mixed up the terminology. I'm expecting to see the attribute present in the page that gets forwarded to, but all attributes are not present - it should be working as I posted above, though?
It should, so the problem lies somewhere else than in the as far provided information. By the way, why are you using GET for this and not POST?
BalusC
Ok update it does work, my url was causing problems. I am using POST on my real webapp, just did the example in get() because it was the first method in my servlet, bad writeup by me.
If you had followed the given links, you'd already found out the answer yourself :) Regardless, I updated this answer to include the relevant information for this particular problem.
BalusC