GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to call a servlet doGet or doPost method inside a JSP page on page load Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call a servlet doGet or doPost method inside a JSP page on page load" Watch "How to call a servlet doGet or doPost method inside a JSP page on page load" New topic
Author

How to call a servlet doGet or doPost method inside a JSP page on page load

Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

I am trying to get some data from a business logic class via a servlets doGet method which I need to call on JSP page load. Whats the best way to do it.

I am trying to draw a graph of votes using table <td width> attributes.

Thanks,
Mo
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

You're doing it backwards. The servlet should should be executed first, and when it has its data ready forward to the JSP to format the view.

Read this article for more details.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pongsakorn Semsuwan
Ranch Hand

Joined: May 15, 2011
Posts: 34

Servlet's doGet is actually means what you want it to do when user send a GET request, not to get data from it. The same applies to doPost.

To send data to JSP from Servlet, use


and then in your jsp,


I suggest you to read about MVC pattern for better understanding of good flow of web application.


A web application developer wannabe from Thailand
My StartUp : http://www.dreamlog.co
Blog : http://pongsakornsemsuwan.wordpress.com
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Pongsakorn Semsuwan wrote:



I don't even use code in my jsp pages anymore thanks to feedback from people on this site! Use EL and JSTL to access your attributes. eg:

<p>This is a HTML/JSP paragraph and this is your attribute: ${attribute_name} </p>
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

Ah, I see now. So you I was indeed doing it backwards. I am still at work and havent implemented the following solution yet but I would assume I am able to set say a servlet as my welcome file in my deployment descriptor and then catch it in my JSP page.

Also, I am new to all these technologies and I will be soon be looking at JSTL and other more up to date used technologies. For now I need something that works.

Thanks a lot for your help everyone.

Best regards,
Mo

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

JSTL and EL work. There's no need to be using outdated and obsolete technologies just because you are a novice. Don't spend time learning bad habits that need to be unlearned later.
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Hi,

Yes you are correct. I will look into EL and JSTL.

Well, I tried to implement the above solution i.e. to set a servlet as the welcome file but that really did not work as I thought it would.

Here is my web.xml file:


I am using Jetty with maven so when I use the following command mvn jetty:run, as opposed to getting my index.jsp I now get a directory listing page instead. When I had my index.jsp set as the welcome file, it navigated to the jsp page but when setting the servlet, unless if I manually navigate to the servlet I am not able to generate and then throw the data to my jsp page.

I guess I am asking, when setting a servlet as the welcome file / page, how can you, say, force your servlet to actually run and navigate to the jsp page as opposed to navigating to the servlet which then dispatches to the jsp page?

Currently my servlet has a doGet method - perhaps this is why? Do doGet not run automatically?

Servlet:


Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Mohammed Uddin wrote:I guess I am asking, when setting a servlet as the welcome file / page, how can you, say, force your servlet to actually run

By correctly setting it as the welcome file. Also, yes should be a GET. Why would a POST display a JSP?
Mohammed Uddin
Ranch Hand

Joined: Mar 04, 2012
Posts: 31
Thanks for your help everyone.

I have been using the MVC pattern and to better implement the welcome file scenario I now have a HTML landing page which when clicked on calls my servlet, does all initialisation, and then dispatches to my index.jsp page.

I treasure the day I joined this forum, it's always been a big help.

Thanks to all,
Mo
 
GeeCON Prague 2014
 
subject: How to call a servlet doGet or doPost method inside a JSP page on page load