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"));
LVL 4
aaaaaaAsked:
Who is Participating?
 
objectsCommented:
You jsp page is run on the server, so you cannot get the client date at that point.
You instead need to add javascript to the page that gets the client date and posts it to the server as bloodredsun has demonstrated.
0
 
objectsCommented:
You can add javascript to your page the same way you add html.
0
 
aaaaaaAuthor Commented:
illegal start of expression
session.setAttribute("date",<script>new Date().toLocaleString()</script>);
                            ^
like above error
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
objectsCommented:
page text needs to be a string:

session.setAttribute("date","<script>new Date().toLocaleString()</script>");
0
 
aaaaaaAuthor Commented:
nothing come out!
0
 
objectsCommented:
are you adding it to your page?
0
 
aaaaaaAuthor Commented:
<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
0
 
objectsCommented:
Your jsp (and your session) run on the server, and your javascript is run on the client.
0
 
aaaaaaAuthor Commented:
so? what ru going to tell?
0
 
objectsCommented:
can u explain what it is you are trying to do?
0
 
aaaaaaAuthor Commented:
i am trying to pass a javascript date into a session valiable and display it out, that is!
0
 
objectsCommented:
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.
0
 
bloodredsunCommented:
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.
0
 
aaaaaaAuthor Commented:
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.
0
 
bloodredsunCommented:
aaaaaa,
>>the method above i know, but that is server date, not use for me.

as both myself and objects have mentioned, the second method DOES use the JavaScript date shich makes it a valid solution for your question. If you follow it carefully, you can see that the form submits the JavaScript date to itself.

The other (IMHO better) way would be to use the server date but formatted for the users Locale, which gives you the same as the client date (as mentioned earlier).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.