简体   繁体   中英

How to display a div AFTER redirecting to next page?

I'm working on my FIRST webapp, using jsp, javascript, etc. The initial page is the "login.jsp". Inside that page, there is a link to the sign up page and when the user submit the registration form, through a Servlet, he is sent back to the login page. But i wish a success alert to appear inside the login page at the same time the page loads.

I don't know the exact way to make this possible. I thought about setting a request attribute like a "notifier" in the servlet, so the login page knows whether the user comes from sign up or not. But then, i'm not sure how to "read" that attribute. I've already tried using javascript, jsp, etc. etc. Nothing worked for me. Besides, I don't know if i should redirect using RequestDispatcher.forward() and set the notifier as a Session attribute. (Here i have another problem, the login page's stylesheet doesn't load.)

This is some of the sign up page:

//....
<form action="/servlet/UpdateUser" method="POST" accept-charset="utf-8" class="form" role="form">
//....        
   <button class="btn btn-lg btn-block signup-btn" type="submit" name="signup">Create my account</button>
</form>
//....

Some of the servlet:

 //servlet
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        if (req.getParameter("signup") != null) {
            User user = new User();
            //user setters and register
            //..
            //Should i use (?):
            //req.setAttribute("SignUp", true);
        } //..
        //rest of the servlet
        resp.sendRedirect("../login.jsp");
    }

Some of login page:

//Rest of Login Page
//Here is something i've tried.
<script type="text/javascript">
    window.onload = function () {
        var b = <%request.getAttribute("SignUp");%>;
        if (b) document.getElementById('success').style.display = 'block';
        else document.getElementById('success').style.display = 'none';
    }
</script>
//Rest of Login Page
<body>
 //..
   <div class="alert alert-success" id="success">
       <strong>Success!</strong> You can now login.
   </div>
//..
</body>
//Rest of Login Page

PD: It's my first question here.

Good job working on your first webapp!

You can definitely communicate weather user just registered or not through a request attribute. The thing is, if the page login.jsp is located inside WEB-INF , you'll have to use request.getRequestDispathcer("../login.jsp").forward(request, response) . If it's in the root, you can use resp.sendRedirect("../login.jsp"); .

The commented code where you set the request parameter SignUp looks fine.

Other thing is your inappropriate use of scriptlets. Namely, when you want to assign a value to var b , you need to use <%= (compared to <% that you used) to return a value. Even better, you can use expression language - just put ${SignUp} after var b = (try to type attribute labels in all lowercase in future).

The rest of JS looks fine.

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