• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Can a jsp page update itself using a servlet ?

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??


 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 85
MySQL Database Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can do this by using the RequestDispatcher's methods or SendRedirect's methods.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will try this and come back ...
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can call static functions from a servlet like you can run any other code in any other program.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic