Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Add an actionListener ActionEvent in jsp

Thread view: 
lynch_susan - 30 Mar 2006 16:36 GMT
Newbie,

Im trying to add an actionListener ActionEvent to a submit button in
jsp and don't know how to go about it.

<form name="form" method="post" action=""
enctype="multipart/form-data">
<input type="file" name="imagefile">
<br>
<input type="submit" name="Submit" value="Submit">
<%
Submit.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == Submit )
    {
        //code to copy image   
               }
}
%>
</form>

Thanks.
Bjorn Abelli - 30 Mar 2006 17:08 GMT
> Newbie,

I understand that you're a newbie, as you have missed an important
difference between JSP and "plain" applications.

> Im trying to add an actionListener ActionEvent to a submit button in
> jsp and don't know how to go about it.

JSP renders HTML-pages to be sent to the browser on the client side, and
responds to requests done to the server. This makes it impossible to add any
meaningful Java-code to be executed on the client-side.

What you usually do, is simply to respond to the submitted request. This
could e.g. be another page, or the same page:

 <form method="post" action="somePage.jsp">

...and then in somePage.jsp, handle any parameters you've recieved.

 String what = request.getParameter( "literal string of field name");

 if ( what.equals("something") ) {

   // Do whatever...
 }

// Bjorn A

-------------
Get FREE newsgroup access from http://www.cheap56k.com
Chris Smith - 31 Mar 2006 17:46 GMT
> Im trying to add an actionListener ActionEvent to a submit button in
> jsp and don't know how to go about it.
[quoted text clipped - 15 lines]
> %>
> </form>

JSP is not AWT, and you can't use the same techniques as you may be
accustomed to using in AWT.  There are a few things that you might want
to do:

1. If JavaScript is sufficient to do what you want, you can add a
JavaScript handler for the "onclick" event.  This is outside the scope
of Java, and is better discussed in comp.lang.javascript.

2. You can fill in the "action" attribute of your form with some URI
that maps to a servlet.  Whatever code you wanted to write in
actionPerformed, you should then write in the servlet's doPost method
instead.

3. If you really like the event/listener kind of organization, you can
look into JavaServer Faces (JSF).  Then you actually could add something
called an ActionListener to the button (though it would be a button
declared with <h:commandButton> instead of <input type="submit">) and
write a listener class that handles things.

The main point, though, would have to be that web applications are a
very different environment from client-side apps, and you just can't
expect programming ideas from one to translate cleanly into the other.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.