开发者

Hyperlink to servlet

开发者 https://www.devze.com 2023-03-24 17:32 出处:网络
I have a java program that accesses a database with columns ID, FirstName, LastName, Age. (ID is the primary key, no two elements in the database have the same ID).

I have a java program that accesses a database with columns ID, FirstName, LastName, Age. (ID is the primary key, no two elements in the database have the same ID).

In part of my program, a Servlet displays all of the elements in the database in a HTML table, exc开发者_如何转开发ept it only displays the ID and FirstName-fields. I have set up the ID field to be a hyperlink. So far so good.

However, when the user clicks the ID hyperlink, I want this to forward them to a Servlet that displays ID, FirstName, LastName, and Age of the particular element they clicked on. So, let's say this is the table the HTML displays:

  • Row 1: [ID=1, FirstName=Jim], and
  • Row 2: [ID=2, FirstName=Joe].

The actual data in the database is:

  • Row 1: [ID=1, FirstName=Jim, LastName=Smith, Age=25], and
  • Row 2: [ID=2, FirstName=Joe, LastName=Chang, Age=37].

In the display table ID fields are hyperlinks for both Jim and Joe. This is all working so far.

However, when I actually click one, it doesn't do what I want. Say I clicked on the hyperlinked 2, in front of Joe. Then I would expect it to display 2, Joe, Chang, 37, from the database, but I don't know how to do this.

All the hyperlink is doing is forwarding the request to a Servlet, whose URL pattern is DisplayPerson.do, and attempting to get the ID parameter so it can get the element with that ID and display all of the other fields.

I know how to display all the fields IF I can get the ID parameter- but I don't know how to set or send different ID parameters when each different hyperlinked ID is clicked, they all just forward to the same Servlet but this doesn't know which one the request was sent from.

I would also like to avoid URL appending if possible, but if that's the only way to do it then I could do that. Can someone explain how this can be done (or even if)?


It looks like with your first response you already have all the stuff you need, then why do you want to send another request to the server? Unless you want to do some totally new/different on the server, a better approach would to use client side javascript to show the stuff you want on click of the URL. Basically, send everything back to the client as the response from your (first) servlet but have the information that you don't want to display hidden at first. Form the URL such that it has onclick event attached to a javascript function. The java script function can accept an identifier or something and then show the stuff dynamically on the client.


So the basic problem is .. but I don't know how to set or send different ID parameters when each different hyperlinked ID is clicked, they all just forward to the same Servlet but this doesn't know which one the request was sent from. ..

How are the hyperlinks being created? They need to include the id associated with the particular record. So the hyperlink should look like 'http://yourservername/yourservletname?id=123'

While creating the URL int he first step, you need to embed the id parameter in the link itself. That should make it work.

Another tip is to install firebug in your browser and see how the request is being sent.


if you dont want to append parameters to the link, an alternative would be to declare an html form for the table (form.method=post and form.action=DisplayPerson.do). assuming you only want to pass one param, personid in this case

  1. for each row, make the link call a javascript function, taking the personId associated with the row as parameter, for example postViewPerson('1'), where the parameter '1' is dynamically generated
  2. have a special hidden input named as clickedPersonid
  3. the js function (ex. postViewPerson) will assign the parameter value to the clickedPersonid and will submit the form
  4. the hidden input now has a value which the servlet can get through request.getParameter("clickedPersonid")


Two Options:

  1. Append parameter to URL, but you dont want to do it.

  2. Instead of link, create submit buttons, with multiple forms for each record where you can even specify POST for your request.

0

精彩评论

暂无评论...
验证码 换一张
取 消