Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

submit using scriptlet  RSS feed

 
Troy Archbold
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp page that calls a scriptlet from within when a submit button is clicked. The problem I am having is that whenever the jsp page is refreshed the scriptlet is also executed. I only want the code to be called when the submit button is clicked.

Any ideas on how to do this would be greatly appreciated. Below is my code.


<%@ page contentType="text/html; charset=iso-8859-1"
language="java"
import="java.io.*,
com.build.AntRun"
%>

<jsp:useBean id="AntBean" class="com.build.AntRun" scope="session"></jsp:useBean>
<html>
<head>
<title>Application Server Bounce</title></head>
<body
<%
if ((request.getParameter("BounceServerA") != null)) {
session.setAttribute("submitted", "yes");
System.out.println("==========it is now working");
}
%>
<form name="form1" method="post" action="BounceServer.jsp">

<p>Select the server application to bounce. </p>
<input type="Submit" name="BounceServerA" value="Bounce Application on Server1" >

</form1>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cannot be done. Scriplet code always executes on the server when the HTML page is being composed to send to the client. Once on the client, only HTML and Javascript remain. Any action that is to take place upon a user event must either be Javascript or a submission back to the server.
 
Troy Archbold
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Do you know of a solid yet simple way around this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't really told us what it is you are really trying to do. Take a step back and describe what you are actually trying to achieve and we might be able to come up with workable scnearios.
 
Troy Archbold
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will have a JSP page with a submit button that a user should be able to click which will call a scriplet. The scriplet will call a JAVA class that executes an Ant script that bounces a server. I do not want the server to be bounced every time the JSP page is refreshed by the user.

Please let me know if there is more info I should provide. Thanks for helping me with this--I am somewhat new to JSPs.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you redirect back to the page after your scriptlet is processed all of the post data will be gone. Then if the user refreshes, they're using a GET that won't still have the form data in it.

NOTE: I had to pull your bean declaration and import..
 
Troy Archbold
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works very well--thank you! Let's assume I need to call additional code in the scriptlet, thus I will need the import and bean declaration. Is that possible with the refresh functionality? The additional code will call a Java class.

<%
if (request.getParameter("BounceSIT3112") != null) {
AntRun ar = new AntRun(); //additional line
System.out.println("I should not be called on refresh!!");

// Use a redirect to dump the post data.
response.sendRedirect("BounceServer.jsp");
return;
}
%>

I think another option I have is to check the user's session ID to verify that the ID has not yet submitted data in the session. session.getId()
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A redirect will generate a GET request. Unless you have querystring parameters, no params will be sent along with the request.

It will be as if you just typed the URL of the page into your browser's address window.
 
Troy Archbold
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you have to pull the bean declaration and import to make the refresh work? Does this mean I cannot have the refresh, bean, and import on one jsp page? Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I only pulled them so the JSP would compile and run on my machine.
(it was quicker than implementing the bean )
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!