wood burning stoves 2.0*
The moose likes Servlets and the fly likes Calling controller servlet form a jsp page through an anchor tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling controller servlet form a jsp page through an anchor tag" Watch "Calling controller servlet form a jsp page through an anchor tag" New topic
Author

Calling controller servlet form a jsp page through an anchor tag

Anas Nashroel
Greenhorn

Joined: May 21, 2008
Posts: 12
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

Joined: Mar 26, 2007
Posts: 130
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 ]

Regards,
Durga Prasad
Anas Nashroel
Greenhorn

Joined: May 21, 2008
Posts: 12
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

Joined: Mar 26, 2007
Posts: 130


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

Joined: Mar 26, 2007
Posts: 130
You may want to look at this Link for more information.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

<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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling controller servlet form a jsp page through an anchor tag
 
Similar Threads
Tomcat manager problems
Failed to load or instantiate TagLibraryValidator
Simplest App ever - problem
Error while running ServletContextListener
Browser Path Not Refreshing