Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoking Scriptlet from HTML button  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible to invoke a scriptlet (that sets data on the servlet context (using application object)) from an HTML button press?

Thanks for any help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

Firstly, scriptlets are a thing of the past and should no longer be used in new code.

Secondly, once the JSP executes and sends the HTML to the browser, it's done and gone. Please read this article to understand how JSPs operate.

If you want to invoke server-side actions as the result of a client-side action (like clicking a button), you need to either submit the page, or employ Ajax.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats interesting that you say that, cause I'm using ajax.

But thats why I was trying to do this.
I'm basically building a chat application.

I use ajax to send request to servlet, which waits for a data to be added, then printouts the added data.
So there is loop in the servlet like

so while that loop runs, entering message on jsp page, sends another call to same servlet by ajax with the message as a requestParameter

The data on Servlet is updated.
So at this point there are 2 threads from servlet, the one thats waiting, and the one that sent the message.
The one that sent the message doesn't need to do out.println(...), as the waiting thread can do this.

So both threads should return about the same time, one with "", and one with the a message.

But it only seems to show "" in the jsp page.
I was thinking it might be because they are both returning at the same time.

Which is why I was trying to upload the message data from button instead of posting it to servlet.

it works if I get both threads to return something, but doing that, when a new message is sent by user, shouldnt the same message be printed twice in the html, which it doesn't if you see what I mean.

Let me know if you have any ideas, thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:I use ajax to send request to servlet, which waits for a data to be added, then printouts the added data.
So there is loop in the servlet like...


A sleep loop in a servlet?

What on earth are you trying to accomplish with this?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a way for the clients front end page (jsp page) to be updated when another client enters text.
I suppose their is RMI, but thats sounds too complicated, and it probably wont work.
How else can I do it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By nature, HTTP is a request/response protocol. You cannot change that. You'll need to do what everyone else on the web does: either poll using Ajax for changes, or adopt a server-push technology such as Comet (and be sure to have the heavy infrastructure to support it).
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats wrong with my method?

Theres minimal network traffic, and no drain in performance on the server.

Let me know if you can, thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:Whats wrong with my method?

Besides the fact that it won't work? All that's going to accomplish is to hang up the request and use up one of the connections that the browser can make to server.

If you need a background thread to do something in the web app, a servlet is not the place to do it. And to be honest, I'm still not sure what you are trying to accomplish with it.

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, fair enough regarding the scriptlet, but my code does work, I'm just trying to make it work better as its a bit confusing.

Best way to learn is to experiment with things instead of people telling you your wrong.
I don't see the problem with it, its just a simple chat application.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it that you think that loop is doing for you? It can't return any response. Perhaps you are not showing the whole picture?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What is it that you think that loop is doing for you? It can't return any response. Perhaps you are not showing the whole picture?

Perhaps the missing part of the picture is that "checkIfDataAdded()" has side effects, such as writing to the response. That isn't what the name of the method suggests, but it wouldn't be the first misnamed method ever to be written. To do that would require some gyrations for the method to get a reference to the response, but that wouldn't be impossible either.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!