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
This is certainly wrong:
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 JavaScriptonclickhandler – will never work: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
onlickhandler in JavaScript and modify the DOM somehow to print current date.