3

I want to use scriptlet to write the function called when clicking the Execute Test button This code didn't work :

Here's my jsp code :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
  <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
 </head>
 <body>

<html:file properties="tonFichier" name="tonForm"/>
<%!

 public void executeTest() {

  java.util.Date d = new java.util.Date();
  System.out.println(d.toString()); } 

 %>

 <form enctype="multipart/form-data" method="get">


  <div>
  <input type="submit" value="Execute Test" onclick="executeTest()" >

  </div>
   </form>
  </body>
   </html>

Any help please Cheers

Amira Manai
  • 2,599
  • 8
  • 40
  • 60

3 Answers3

4

You can do as follows.

if(request.getParameter("btnSubmit")!=null) //btnSubmit is the name of your button, not id of that button.
{
    java.util.Date d = new java.util.Date();
    System.out.println(d.toString()); 
}

<input type="submit" id="btnSubmit" name="btnSubmit" value="Execute Test"/>

onclick="executeTest()" with your button tries to invoke a Javascript function. Change your button tag as mentioned in the above code and encolse this code within a sciptlet. It will do the job when you click this button.

Additionally, you may want to replace

System.out.println(d.toString()); 

with

out.println(d.toString()); 

in your code.


Also, in your form tag,

<form enctype="multipart/form-data" method="get">

the attribute enctype="multipart/form-data" is required when you're uploading files. You should remove it, if such is not a case and

method="post"

The form attribute enctype="multipart/form-data" can not work, if you use method="get"

Lion
  • 18,729
  • 22
  • 80
  • 110
  • Thank you so much , but in my case i want to use java code because i want to execute a maven command after clicking on the button is it possible to execute commands using javascript?? – Amira Manai Jul 08 '12 at 19:39
  • 1
    @Amira Manai:) You can surely use Java code to execute `maven` commands but you can't using Javascript because Javascript is a client-side scripting language running on a browser (and not on a server as done with Java). – Lion Jul 08 '12 at 19:55
1

I think you're confusing Java functions and Javascript functions i.e., server-side vs client-side.

shawnt00
  • 16,443
  • 3
  • 17
  • 22
0

We cannot call the functions which are written in jsp page using java. We can use the javascript for creating events and submit the values to the another jsp page for further processing.