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

1
  • At a glance, there is no event handling mechanism supported by JSP (or Servlet). Commented Jul 8, 2012 at 19:19

3 Answers 3

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"

Sign up to request clarification or add additional context in comments.

2 Comments

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:) 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).
1

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.