Link to home
Start Free TrialLog in
Avatar of aaaaaa
aaaaaa

asked on

display javascript in jsp

how to display a date(javascript) in a jsp ?


like:
session.setAttribute("date",<script>new Date().toLocaleString()</script>);
out.println(session.getAttribute("date"));
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You can add javascript to your page the same way you add html.
Avatar of aaaaaa
aaaaaa

ASKER

illegal start of expression
session.setAttribute("date",<script>new Date().toLocaleString()</script>);
                            ^
like above error
page text needs to be a string:

session.setAttribute("date","<script>new Date().toLocaleString()</script>");
Avatar of aaaaaa

ASKER

nothing come out!
are you adding it to your page?
Avatar of aaaaaa

ASKER

<script>new Date().toLocaleString()</script>

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
Your jsp (and your session) run on the server, and your javascript is run on the client.
Avatar of aaaaaa

ASKER

so? what ru going to tell?
can u explain what it is you are trying to do?
Avatar of aaaaaa

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.
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("aDate" , new Date() ) ;
<script language="JavaScript">
      alert("The session date is: <%= session.getAttribute("aDate")%> ") ;
</script>

To do it the way you were requresting means you need to do this:

<%@ page language="java" %>
<html>
<head><title>javascript/session date example</title></head>
<body>
<%
String paramDate = request.getParameter("myDate") != null ? request.getParameter("myDate") : "" ;
session.setAttribute("myDateInSession" , paramDate) ;
%>

<script language="JavaScript">
function clickAndSubmit(){

      document.getElementById('myDate').value = 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("myDateInSession")%>
</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.
Avatar of aaaaaa

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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial