[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

display javascript in jsp

Posted on 2005-04-17
17
Medium Priority
?
350 Views
Last Modified: 2010-04-01
how to display a date(javascript) in a jsp ?


like:
session.setAttribute("date",<script>new Date().toLocaleString()</script>);
out.println(session.getAttribute("date"));
0
Comment
Question by:aaaaaa
  • 7
  • 6
  • 2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13803278
You can add javascript to your page the same way you add html.
0
 
LVL 4

Author Comment

by:aaaaaa
ID: 13803287
illegal start of expression
session.setAttribute("date",<script>new Date().toLocaleString()</script>);
                            ^
like above error
0
 
LVL 92

Expert Comment

by:objects
ID: 13803291
page text needs to be a string:

session.setAttribute("date","<script>new Date().toLocaleString()</script>");
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:aaaaaa
ID: 13803301
nothing come out!
0
 
LVL 92

Expert Comment

by:objects
ID: 13803304
are you adding it to your page?
0
 
LVL 4

Author Comment

by:aaaaaa
ID: 13803315
<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
 
LVL 92

Expert Comment

by:objects
ID: 13803330
Your jsp (and your session) run on the server, and your javascript is run on the client.
0
 
LVL 4

Author Comment

by:aaaaaa
ID: 13803339
so? what ru going to tell?
0
 
LVL 92

Expert Comment

by:objects
ID: 13803350
can u explain what it is you are trying to do?
0
 
LVL 4

Author Comment

by:aaaaaa
ID: 13803384
i am trying to pass a javascript date into a session valiable and display it out, that is!
0
 
LVL 92

Expert Comment

by:objects
ID: 13803394
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13804430
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
 
LVL 4

Author Comment

by:aaaaaa
ID: 13804559
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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13804579
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
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 1000 total points
ID: 13805007
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Currently, there is an issue with being able to copy values from an external application to a dropdown list in Project Web Access (PWA).  The standard copy and paste methods don't seem to work properly. Here is a way to accomplish this task to s…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question