0

everyone.

I am looking for a way to add a parameter to a url that linked directly to a servlet. That is, the application that I am creating uses one Servlet to route people through the application it self. It looks for a parameter called action, and from that, it decides where the user will go.

The problem is that the link that I am using to link to a dashboard page calls a servlet that I called Router within the href itself. I thought that I could just use the following code to add an action parameter and set it to dashboard:

<a href="Router?action=dashboard">Dashboard</a>

However, this doesn't work. I need a way to add the parameter to the link itself that way I can process which page the user is trying to access.

Here is my code for both the JSP that I am using and the Servlet that is used to route the users around.

<div class="collapse navbar-collapse" id="app-navbar-collapse">
            <!-- Left Side Of Navbar -->
            <ul class="nav navbar-nav">
                <li><a href="index.jsp">Home</a></li>
                 <li><a href="Router?action=dashboard">Dashboard</a></li>
            </ul>

And here os the Servlet code:

String action = (String)request.getAttribute("action");

    User user = (User) request.getAttribute("user");

    String url = "home.jsp";

    boolean user_auth = user.getUserAuth();



    switch(action) {
        case "home":
         request.setAttribute("user", user);
         request.getRequestDispatcher(url).forward(request, response);
         break;
        case "dashboard":
            if(user.getUserAuth()) {
                url = "dashboard.jsp";

                out.println("You've been authed");
                //request.getRequestDispatcher(url).forward(request, response);
            } else {
                out.println("You're NOT authed.");
                //request.getRequestDispatcher(url).forward(request, response);
            }
            break;
        default: 
            request.getRequestDispatcher("404Error.jsp").forward(request, response);
    }

This is a pretty simple and straight forward application, and its meet to be that way since I'm just starting to learn. But I would just like to know how to add the parameter straight to the url in the href.

If there is no way of doing this, can you offer a better solution to this issue? Should I be using a separate Servlet to process this request?

Thanks in advance.

Kaley36
  • 233
  • 9
  • 19

2 Answers2

2

If you're setting a parameter in the link, you can't use getAttribute() in your Servlet. Probally, you're getting a NullPointerException.

What you must to is use getParameter("action") instead.

Andrew Ribeiro
  • 666
  • 1
  • 7
  • 18
0

In your case use request.getParameter("action"); and also you can use form and hidden form field to send some static values to servlet.

Hidden form field

<form action="./Router" method="post">
 <input type="hidden" name="action" value="dashboard" />
<button type="submit">Dashboard</button>
</form>
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52