Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling controller servlet form a jsp page through an anchor tag

 
Anas Nashroel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I develop a dummy mvc2 web application for school administration. I created a jsp welcome page (index.jsp)that contains a link to list all student.
From this link, we will be directed to the controller servlet, namely SisdikController.
How can i create this link so the controller servlet gets called ??

Here's my controller servlet:



and here's my web.xml:


In the controller, the request will be routed according to the request parameter given. In this case, i want the request param value to be 1 (VIEW_ALL_STUDENTS). How can I pass this value in my anchor tag ??

Is this form of anchor tag a correct one ??


My development machine:
Windows xp
Tomcat 5.5
JRE 1.5
Thanks in advance..
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anas,

How can i create this link so the controller servlet gets called ??

You Already have an answer for this. The mapping you have done in the DD should hold good for this.

How can I pass this value in my anchor tag ??

You generally do it this way in your case....


Generally, all the input fields in your jsp form will be passed as the request parameters to your servlet.
[ May 28, 2008: Message edited by: Prasad Tamirisa ]
 
Anas Nashroel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prasad for a quick response...
I did manage to pass the request to the controller, and this controller passes the job to another method to do some processing (querying the database to select all students).
I then tried to forward the request object which contains studentColl attribute. This attribute contains the collection of student objects queried from the database.

I found no error until I tried to access this attribute from jsp page, student-list.jsp.
It throws these error:


here's the student-list.jsp code:



It seems that, when I tried to access the studentColl, it throws NullPointerException

Is the way I pass object/bean from controller to the jsp correct ??
If it isn't , how can I pass the beans from controller to a jsp page ??

Thanks...
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you don't specify the scope explicitly in the ussBean tag, the container will assume it as a page scope attribute.

And so, it will throw the exception. Try specifying the scope attribute in the above code...
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to look at this Link for more information.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<a href="/sisdik?reqParam=<your values>">view all students</a>


You may also want to see this FAQ entry on building robust URLs.
As you've written it, this will only work if your application is the root context in your server.

http://faq.javaranch.com/java/ResourceUrlProblems
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic