my dog learned polymorphism*
The moose likes Servlets and the fly likes Can a jsp page update itself using a servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can a jsp page update itself using a servlet ?" Watch "Can a jsp page update itself using a servlet ?" New topic
Author

Can a jsp page update itself using a servlet ?

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Hi folks I am kind of stuck on a little issue here and i was hoping you experts could clear this out..

Simplest Scenario: User gets a webpage with 3 text boxes labeled name,age and city.
The user then presses a button on a page as a result the word "great" gets inserted before each word in the 3 text boxes...

Now I know that the normal routine would be , is to create HTML page with the three text boxes and a submit button.
Once the button is clicked the form would post the data to servlet.
The servlet would then make the necessary string additions to each item in the 3 textboxes and pass the data to an already created jsp page(which has exactly the same html layout as the first page)
once the jsp page receives data from the servlet it then inserts the data into the respective textboxes so the pattern goes like this
page1->servlet->page2(Jsp page with design similar to page 1 only textboxes would be updated)

My question is : is there anyways by which the order can go like this:
step 1:
page1->servlet
step 2:
servlet->page1

I was thinking that page 1 could be a jsp page and when the user has entered his data the jsp calls a function in the java servlet which returns it back a string list or whatever , and data from that list is inserted into the textboxes.. Is this scenario possible ??




Don’t look where you fall, but where you slipped
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Of course. No problem.

What you will do is update your attributes/parameters to reflect the change in your implemented method (doGet or doPost) and then dispatch your request back to the jsp.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Ifteqar Ahmed
Ranch Hand

Joined: May 02, 2011
Posts: 85

Yes you can do this by using the RequestDispatcher's methods or SendRedirect's methods.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Will try this and come back ...
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Okay here is what i came up with so far the java class is this


package com;


and the jsp file is this

//File name = "Test.jsp"


Question : Since i am calling a static function of a servlet do i have to register this in web.xml ?
Question : Now how can i access the static function "process" when the user clicks the "Process" button
any help would be appreciated.. thanks..
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Be advised that this code will cause a massive explosion and millions will die from the fallout.

Why don't you just put the static code you wrote there in the doGet method of your servlet instead of a static method?

You're also going to need a form in your jsp.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Dieter Quickfend wrote:Be advised that this code will cause a massive explosion and millions will die from the fallout.

Why don't you just put the static code you wrote there in the doGet method of your servlet instead of a static method?

You're also going to need a form in your jsp.


LOL.. Thanks for pointing that out..but i am just curious on how this could be achieved and why would this cause a massive explosion ??
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Dieter Quickfend wrote:
Why don't you just put the static code you wrote there in the doGet method of your servlet instead of a static method?
You're also going to need a form in your jsp.


I have already done that what i am instrested in is calling a static function as mentioned above ..
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

You can call static functions from a servlet like you can run any other code in any other program.
Dinesh Arora
Greenhorn

Joined: Apr 18, 2007
Posts: 21
Use the requestDispatcher method to get to the same page.

Page1 -->Send params to servelet ---> Servlet appends the word"great" before params and then use requestDisptacher or sendredirect to get back to page.

Second option: Just use Ajax call to send the request and get the response back.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

You cannot call a static methiod (and what a poor paractice thing to try to do) from the HTML.

In fact, from your description why do you even need to go back to the server at all? If all you want to do is affect the display with extra text, just do it with JavaScript on the client and be done with it.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

I know the following is a very bad method/practise but still i am just curious about this method, and i would like to run it..

Here is what i could come up with... in my jsp page



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

If you know it's bad, what's the purpose of pursuing it?

First of all, scriptlets? In 2011?

Secondly, in your first post you said "after a button is pressed" (or some such). How is embedding Java in a JSP that executes on the server long before the page is even sent to the browser, going to have anything to do with a button press?

There are modern patterns and best practices to do just about anything you need to do in a web application. You are heading astray. Badly.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:If you know it's bad, what's the purpose of pursuing it?

First of all, scriptlets? In 2011?

Secondly, in your first post you said "after a button is pressed" (or some such). How is embedding Java in a JSP that executes on the server long before the page is even sent to the browser, going to have anything to do with a button press?

There are modern patterns and best practices to do just about anything you need to do in a web application. You are heading astray. Badly.


what would you suggest in 2011?? I just started JSP and servlets.. what do you suggest ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

First of all, if you are new to JSP and servlets, read this article and this article.

Secondly, avoid anything to do with scriptlets. They've been discredited for almost 10 years now. A decade. It's completely irresponsible to be putting scriptlets into modern JSP code.

Next, understand that doing anything on the client involves JavaScript, not JSP and servlets. So any question that contains "press a button" is likely to involve JavaScript unless it's a straight-forward form submission. If server-side assist is required, that's where Ajax comes in.

And lastly, learn how to ask non-leading questions. Until you get your feet wet, it's better to ask "how do I do such and such" rather than "how do I use xyz to do such and such" where it's likely that xyz is not the appropriate technology.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:First of all, if you are new to JSP and servlets, read this article and this article.

Secondly, avoid anything to do with scriptlets. They've been discredited for almost 10 years now. A decade. It's completely irresponsible to be putting scriptlets into modern JSP code.

Next, understand that doing anything on the client involves JavaScript, not JSP and servlets. So any question that contains "press a button" is likely to involve JavaScript unless it's a straight-forward form submission. If server-side assist is required, that's where Ajax comes in.

And lastly, learn how to ask non-leading questions. Until you get your feet wet, it's better to ask "how do I do such and such" rather than "how do I use xyz to do such and such" where it's likely that xyz is not the appropriate technology.


Thanks for the suggestions. I am going to shut this post down and start a new thread in which i would like to ask a few jsp questions....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a jsp page update itself using a servlet ?