Reputation: 1322
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?
Upvotes: 1
Views: 569
Reputation: 17839
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.
Upvotes: 1
Reputation: 5766
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>");
Upvotes: 1