0

I have a menu with several categories of books.

When I click on a menu item I want to have a servlet query a database and return the results.

I don;t want to have repeated code, so it'd be nice to just use one servlet, but somehow have the servlet identify which link in the menu was clicked and perform the appropriate actions.

Is the only way to do this by submitting a form?

Also, I should mention that despite being horrible practice, I will be targeting a different frame for the results.

Lebowski156
  • 951
  • 5
  • 11
  • 34

2 Answers2

0

No, you don't need a form submit. Parameters are passed by links using a query string:

<a href="/books?category=novels">Novels</a>
<a href="/books?category=sf">Science Fiction</a>
...    
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

This post is may be old. But here is my answer.
You can create a link like this in jsp page.

 <a href="${pageContext.request.contextPath}/new">Add New Book</a>

 <a href="${pageContext.request.contextPath}/list">List All Books</a>

And call your controller like this.

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getServletContext().getContextPath()+request.getServletPath();

        try {
            switch (action) {
            case "/new":
                showNewForm(request, response);
                break;
            case "/insert":
                insertBook(request, response);
                break;
            case "/delete":
                deleteBook(request, response);
                break;
            case "/edit":
                showEditForm(request, response);
                break;
            case "/update":
                updateBook(request, response);
                break;
            default:
                listBook(request, response);
                break;
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

This is for a servlet that keeps as the startup page.
You can update the href attribute according to the url pattern of the servlet.

Dil.
  • 1,996
  • 7
  • 41
  • 68