1

I have 2 servlets now. I have a navigation bar on the left and a content space on the right. I use the folowing JS in order to put the servlets output in the right spot

    $(document).ready(function() {                        
        $('#navigation').load('Navigation');
        $('#content').load('Content');
    });

Inside the Navigation, I have links in order to get the articles that I want in the content space. So on every categoryname of my navigation, I have a reference to the other servlet. So the navigation is a servlet, and the content is a servlet. The problem is, that the content does not "get" the names of the navigation.

    String category = request.getParameter("category");
    String suche = request.getParameter("suche");

I try to get the category name I click on as an ID, or the search string I type in. Both do not work. A category look like this in the navigation:

out.println("<li><a><form method='get'><button type='submit' name='category' value='"+ k2.getKategorieNr() + "'>" + k2.getKName() + "</button></form></a></li>");

I gues that the problem is that everytime I click on a category, the whole HTML page makes a reload and the name that is inside the navigation servlet, cannot be refered by the content servlet so the getParameter returns always null in the content servlet. I just have no idea how I can design my web app in order to make it work. Any idea?

yemerra
  • 1,232
  • 2
  • 16
  • 39

2 Answers2

1

Put the value for "category" into a hidden field rather than on a button:

out.println("<li><a><form method='get'><button type='submit'>" + k2.getKName() + "</button><input type='hidden' name='category' value='"+ k2.getKategorieNr() + "'/></form></a></li>");
garryp
  • 4,915
  • 1
  • 23
  • 37
  • Yes the form does not post the value inside the value attribute of button. You can confirm this by running your code and looking in the URL and noting that category is not in the query string. Change it to a hidden field and you will see it. – garryp Apr 30 '15 at 08:33
  • Dennis did this fix your problem? – garryp Apr 30 '15 at 21:44
1

When an html element is submitted there are certain rules that apply. First the form will be submitted to the page defined by the action attribute of the form. Secondly the request to the server will contain the values of any <input> elements.

In your case the action attribute is missing, so you will want to fix your code with something like:

<form action="/servlet1" .... >

Then you have to specify what will be submitted. In your case you have no fields submitted with the form. So in your servlet, the statement request.getParameter("anyAttribute") will always return null. So you have to include in your form some values to be submitted like

<form action="/servlet1" method="get">
  <input type="hidden" name="category" value="categoryname"/>
  <button type="submit">submit</button>
</form>

This code will make a submission of the form to a Servlet mapped in your web.xml as servlet1, and you can get the category value in the doGet() method of the Servlet using request.getParameter("category").

Extending your functionality you could also use ajax submission in order not to reload the page every time. I would recommend the ajax-jQuery implementation to do that.

Community
  • 1
  • 1
MaVRoSCy
  • 16,907
  • 14
  • 76
  • 116
  • The problem I have is, that my other servlet that takes the parameter of this navigation is a new page. I have the result now, no problem. But it is not inside the content space, it is a completly new page. – yemerra Apr 30 '15 at 11:49