newbie question

Hi guys

I have a jsp page where i want to display a table that is populated with database entries. I try to maintain a MVC architecture and call the database in the doGet in a servlet and the jsp page then needs to call the servlet to receive the data. I know how to do this using a form and a submit button, however I do not wish to have a button to call this. Since I'm a newbie in jsp development I'm not sure what is the usual way to do this. Can anyone please help me?

Thanx

//kira

[514 byte] By [kiraa] at [2007-10-2 11:46:01]
# 1

Hi,

When you call your database in the servlet, you probably return it in some List-format. (eg myList) If "myList" is a list of Person objects there would be a getFirstName() method, right?

in the servlet:

request.getSession().setAttribute("myList", myList);

in the jspfile:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%-- jstl 1.0 --%>

<c:forEach items="${myList}" var="listElement">

<c:out value="${myList.firstName}"/>

</c:forEach>

SomeThingWeirda at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi, and thanx for your reply :)

In the servlet I call the database and put the resultset in a collection which i return to the jsp. In the jsp i then get this collection from the request attribute. And this is working well, that is i can successfully receive the information from the servlet and display it on the jsp page using somewhat the same code you show above.

However my problems lies in how or where to call the servlet from the jsp page. currently I am using a submit button on the form to call the servlet but I would very much like to get rid of this button and am wondering what other ways there are to do this (without using the submit button). I was thinking maybe I could do something in the "onLoad" functionin the body, but I don't know whats best really, any ideas?

bezt regardz

//kira

kiraa at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

ok i've been thinking. if i have like a menu on my pages then it probably sounds most logical to call the servlet when i select the list page in the menu

the menu item would look something like this:

<li ><a href='listBooks.jsp' onclick="getData()">Overview</a></li>

where getData is the javascript like this:

function getData()

{

ListBooksServlet s = new ListBooksServlet () ;

s.doGet( request, response );

}

but unfortunately somehow this doesn't seem to be working

any ideas ?

best regardz

//kira

kiraa at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

For this to work, you need to point your href to the resource your requesting (servlet), drop the onclick attibute, and have that servlet return to a view component OR

have the javascript function create/send a request object ala AJAX.

Either way, I do not think

<a href='listBooks.jsp' onclick="getData()">

is going to work for you in this scenario.

-S.

sroota at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thank you so much for your reply :)

I already tried to call the servlet in href but that didnt seem to work for me (i guess i must have done it wrong *embarrassed*)

In my ignorance I tried:

<a href='/WEB-INF/classes/web/ListBooksServlet'>

<a href='/WEB-INF/classes/web/ListBooksServlet?'>

<a href='/WEB-INF/classes/web/ListBooksServlet.class'>

<a href="/WEB-INF/classes/web/ListBooksServlet">

However I always got a 404 error telling me the requested resource was not available.

Came across another thing that i wasnt too sure of, that is that I have two web-inf directories one for deployment (exploded) and one on project level. they both contained the web.xml, however only the deployment one contained the classes directory so i tried copying the classes dir to the other web-inf but still it didn't find anything :/

Any ideas?

//Kira

kiraa at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
problem solved :)it worked when using a servlet mapping thank you all for being so helpful//kira
kiraa at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi Kira,

The reason I didn't tell you about contacting the servlet from your jsp, is because it's not a architectual correct way. Its bad design.

You should let the controller part do the logic (with help from your model) and then let the controller (your servlet) prepare data for your jsp by placing them in the request-scope.

Calling the servlet from jsp should not be nessesary.

SomeThingWeirda at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

hi again :)

thank you very much for your comment. i guess my architecture is incorrect then. it looks something like this:

1. views:

several jsp pages that include html code and forms who's action is the appropriate servlet (either through get or set)

2. controllers:

several servlets that call the model classes and redirect the upcoming data to the appropriate jsp page

3. models:

java classes that represent the objects and get and set methods and the sql calls to the db, using the connection object from the connection class

4. database connection class

jdbc connection class

this architecture thus successfully sepperates different parts of coding i thought. but i see i must be missing some kind of one controlling servlet then? hmm but i dont understand, do you then always put this controlling servlet as the servlet in the action of the forms in the jsp pages or what ?

best regards

//kira

kiraa at 2007-7-13 5:53:58 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...