0

I hope you get my problem.

out.println("<li class='has-sub'><a href='#'>" + k1.getKName() + "</a>\n");

I have a JSP and inside this java code. The result is a navigation on the left side with several categories and subcategories. So this is one category element. As you can see, I didn't put anything in the href. What I want to do is, that when I click on this category, I will get the articles of this category in the content space on the right side.

So, what do I have to do with servlets or JSPs in order to give a result to the content space. I can't just call a servlet there of course, because that means that I get the result of the servlet inside the href obviously.

I am sorry if this is a silly question, but I really don't know how to solve this :(

yemerra
  • 1,352
  • 4
  • 19
  • 44

1 Answers1

1

Further to previous comments you do not need web services. You can do this using ajax and a normal Servlet. You might want to look at using JQuery to help with the Ajax part. Here's some JQuery documentation around the load() function which will:

Load data from the server and place the returned HTML into the matched element.

https://api.jquery.com/load/

Your link will look something like (if k1 is a bean in some scope then you can use EL rather than scriptlets):

<a href='javascript:loadData(${k1.id});'>${k1.name}</a>

Your Javascript will look something like:

function loadData(id){
   var url = "/pathToMyServlet?id=" + id;
   $( "#result" ).load( url  );
}

which will call your Servlet and insert the HTML returned to an element on your page with the ID 'result'.

Your Servlet then needs to generate the data and forward to a simple JSP which returns the results (and only the results) i.e. it does not need to be a fully formed HTML page but should only contain the table of results or whatever.

And stop using scriptlets:

How to avoid Java code in JSP files?

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110