1

i want to add a button when i click it the date should be displayed but the button doesn't work .

my servlet:

package test.servlets; 

  import java.io.*;
  import javax.servlet.*;


      public class FunctionalTestServlet extends HttpServlet
        {

public void doGet(HttpServletRequest request,HttpServletResponse response)throws    

        ServletException, IOException {



     response.setContentType("text/html");
      PrintWriter out = response.getWriter();  
      String text = request.getParameter("montext");
      String fileName = request.getParameter("testclass");

      out.println("<b><font color='blue'>The text is :</font></b>" 
      + "<b>"+ text +"</b>" + "<br>");

      out.println("<b><font color='blue'>The File name is :</font></b>" 
              + "<b>"+ fileName+"</b>" + "<br>");


      out.println("<% public void executeTest() {" +

 " java.util.Date d = new java.util.Date();out.println(d.toString()); } %>");
 **out.println("<input type='submit' value='Execute Test'onclick='executeTest()'>");**

    }


}

jsp page :

 <%@ 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"/>

    <form action="FunctionalTestServlet" enctype="multipart/form-data" method="get">
    <p>
  Type some text (if you like):<br>
  <input type="text" name="montext" size="30">
   </p>
    <p>
    Please specify a Test , or a set of tests:<br>
   <input type="file" name="testclass" size="40" >
    </p>
   <div>
  <input type="submit" value="Execute Test">

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

Any idea please Cheers

1
  • What doesn't work, what do you see when you click it? Commented Jul 8, 2012 at 14:20

1 Answer 1

2

This is certainly wrong:

out.println("<% public void executeTest() {" +

You cannot generate scriptlet code in servlet. Scriptlet code belongs to JSP. Any text returned from JSP (sent to out) is sent directly to the client. Thus the HTML delivered to the browser will contain scriptlet code - which should have been evaluated on the server-side!

Scriptlets can only appear in JSP.

To make it even funnier, you are trying to attach Java executeTest() method as a JavaScript onclick handler - will never work:

out.println("<input type='submit' value='Execute Test'onclick='executeTest()'>")

In fact your code is broken is so many ways that it requires complete rethink/rewrite. Start from understanding how, when and where servlets, JSP and JavaScript works.

I kind of get your idea and guess what, it doesn't require neither JSP nor servlets. Just write onlick handler in JavaScript and modify the DOM somehow to print current date.

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

2 Comments

what @tomasz said is completely true, in order to achieve what you want you may use javascript like the following : out.println("<script type='text/javascript'> function f(){ var d=new Date(); document.write(d); } </script> "); out.println("<body><input type='submit' value='Execute Test' onclick='f()'></body>");
Thank you much for the explaination :)))

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.