?
Solved

Convert Java variable to Javascript variable

Posted on 2007-11-27
8
Medium Priority
?
4,060 Views
Last Modified: 2008-02-01
Hi guys,

This is pretty weird. I'm using BEA workshop 8.1. In my JSP, there is a Javascript which I need to convert a Java variable to Javascript variable.

A section of my codes goes below:

<%
String hsCode = request.getAttribute("hsCode");
System.out.println(hsCode);
%>
var JShsCode = <%=hsCode%>;
alert(JShsCode);

In my console, the hsCode is printed out as 0701, but in my alert box when I load the JSP, JShsCode becomes 449. With each different value of hsCode, the JShsCode value changes .e.g. hsCode 077 will alert me JShsCode 63.

Any ideas on this?

0
Comment
Question by:leon24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1200 total points
ID: 20359472
0701 in many languages equates to an octal number - the decimal of which is 449.

Using PHP ...

<?php echo 0701; ?>

outputs ...

449

As both PHP and JS are loosely typed, you can use strings.

<?php echo '0701'; ?>

outputs ...

0701

So try ....
<%
String hsCode = request.getAttribute("hsCode");
System.out.println(hsCode);
%>
var JShsCode = '<%=hsCode%>';
alert(JShsCode);

Open in new window

0
 
LVL 29

Assisted Solution

by:Göran Andersson
Göran Andersson earned 800 total points
ID: 20359498
When you put a zero as the first digit in a numeric literal in Javascript, it's interpreted as an octal number (base 8).

You can either convert the value to a number in the server code, so that it's output into the page without the leading zero, or parse the string with a specific base in the Javascript code:

car JShsCode = parseInt('<%=hsCode%>', 10);
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20359511
Oh. The trigger for an octal number is a leading 0.

You could also left trim all leading zeros.

Not sure on the Java method for that.

You could just read 1 char at a time and convert to a space and then not put the quotes in the JS output.

So, (using _ as a space), 0701 ...

var JShsCode = 0701;

becomes (remember using _ as a space) ...

var JShsCode = _701;


Which removes the octal notation marker.
0
Independent Software Vendors: 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!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20359516
Personally, I would output the correct value from the server rather than getting the client to do the call.
0
 

Author Comment

by:leon24
ID: 20359671
Hi guys,

I've tried using the methods mentioned, I've missed this part out earlier in my question. The hsCode 0701 is a number which the user input, which means it could be 334 or 0802 or 0701 or anything (numeric). And I tried using parseInt, but it doesnt retain the 0 in front, as code 0701 is different from 701.

Any idea?
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 1200 total points
ID: 20359699
You say "number" ...

0701 === 449 === 0x1c1

They are the same thing, just different representations.

I think you really mean string.

If so, use the attached snippet.

Don't try to convert the text the user entered.

You may want to validate that the string only contains digits (regex /^[\d]+$/ for example - change + to * to allow blank).
<%
String hsCode = request.getAttribute("hsCode");
System.out.println(hsCode);
%>
var JShsCode = '<%=hsCode%>';
alert(JShsCode);

Open in new window

0
 

Author Comment

by:leon24
ID: 20359700
OK guys I've solved it oredi and using '<%=hsCode%>' works fine and retains the leading 0 in front. You guys are cool s**t!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 20359716
Well, we are Experts anyway!
0

Featured Post

Independent Software Vendors: 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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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