aaaaaa
asked on
display javascript in jsp
how to display a date(javascript) in a jsp ?
like:
session.setAttribute("date ",<script> new Date().toLocaleString()</s cript>);
out.println(session.getAtt ribute("da te"));
like:
session.setAttribute("date
out.println(session.getAtt
You can add javascript to your page the same way you add html.
ASKER
illegal start of expression
session.setAttribute("date ",<script> new Date().toLocaleString()</s cript>);
^
like above error
session.setAttribute("date
^
like above error
page text needs to be a string:
session.setAttribute("date ","<script >new Date().toLocaleString()</s cript>");
session.setAttribute("date
ASKER
nothing come out!
are you adding it to your page?
ASKER
<script>new Date().toLocaleString()</s cript>
come out nothing
<script>document.write(new Date().toLocaleString())</ script>
come out this: Monday, April 18, 2005 10:19:15 AM but display in top of the page.
i want it in session value
come out nothing
<script>document.write(new
come out this: Monday, April 18, 2005 10:19:15 AM but display in top of the page.
i want it in session value
Your jsp (and your session) run on the server, and your javascript is run on the client.
ASKER
so? what ru going to tell?
can u explain what it is you are trying to do?
ASKER
i am trying to pass a javascript date into a session valiable and display it out, that is!
you cannot store a javascript date in the session, as the session is stored on the server.
you can only store the javascript code as you are doing above.
you can only store the javascript code as you are doing above.
The only way you could transfer the javascript date into the session is by submitting that information in a form and then adding it to the session in the form handling jsp/servlet.
As objects has correctly pointed out, Javascript runs on the client and JSP on the server. This means that it is very easy to pass information from the JSP to the JavaScript but not the other way around
if you want to put a date in the session then you would use:
session.setAttribute("aDat e" , new Date() ) ;
<script language="JavaScript">
alert("The session date is: <%= session.getAttribute("aDat e")%> ") ;
</script>
To do it the way you were requresting means you need to do this:
<%@ page language="java" %>
<html>
<head><title>javascript/se ssion date example</title></head>
<body>
<%
String paramDate = request.getParameter("myDa te") != null ? request.getParameter("myDa te") : "" ;
session.setAttribute("myDa teInSessio n" , paramDate) ;
%>
<script language="JavaScript">
function clickAndSubmit(){
document.getElementById('m yDate').va lue = new Date() ;
document.forms[0].submit() ;
}
</script>
<form name="form1" action="date.jsp" method="GET">
<input type="hidden" name="myDate" id="myDate"/>
</form>
Click <a href="#" onclick="clickAndSubmit()" >here</a> to set the javascript date into the session<br>
<p>
Here's a javascript date from session: <%= session.getAttribute("myDa teInSessio n")%>
</p>
</body>
</html>
So you can see that the anount of effort required to do this is far more than that required to use Java to set the session vairable directly in the jsp/session.
As objects has correctly pointed out, Javascript runs on the client and JSP on the server. This means that it is very easy to pass information from the JSP to the JavaScript but not the other way around
if you want to put a date in the session then you would use:
session.setAttribute("aDat
<script language="JavaScript">
alert("The session date is: <%= session.getAttribute("aDat
</script>
To do it the way you were requresting means you need to do this:
<%@ page language="java" %>
<html>
<head><title>javascript/se
<body>
<%
String paramDate = request.getParameter("myDa
session.setAttribute("myDa
%>
<script language="JavaScript">
function clickAndSubmit(){
document.getElementById('m
document.forms[0].submit()
}
</script>
<form name="form1" action="date.jsp" method="GET">
<input type="hidden" name="myDate" id="myDate"/>
</form>
Click <a href="#" onclick="clickAndSubmit()"
<p>
Here's a javascript date from session: <%= session.getAttribute("myDa
</p>
</body>
</html>
So you can see that the anount of effort required to do this is far more than that required to use Java to set the session vairable directly in the jsp/session.
ASKER
what i want is client date, so i use javascript date.
the method above i know, but that is server date, not use for me.
the method above i know, but that is server date, not use for me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.