简体   繁体   中英

Value mismatch while passing attribute from Servlet to jsp

Setting value in Servlet and retrieving it in jsp using RequestDispatcher

In test1Servlet :

request.setAttribute("Alpha",alpha);

RequestDispatcher rd = request.getRequestDispatcher("Test.jsp");

rd.forward(request, response);

In Test.jsp: On click of button am doing ajax call to another servlet with request attribute values

$(document).ready(function(){

  $(".button1").click(function(){
            $.ajax({

  type: "POST",

  url: "<%=request.getContextPath()%>/test2Servlet", 

 data: {"alpha":<%=request.getAttribute("alpha")%>,"ins":10},

 dataType:"text",

In this case if I set alpha value as 0017 in test1Servlet, am getting 0015 in jsp.

How this value is getting changed ?

Anyone please help me to resolve this.

It is no error there. You get the right value.
In many languages number preceded by zero are interpreted as octals or hexadezimal:

  • OCTAL: 017 = 15
    (because the 1 is evaluated to 8 an the 7 as it is. So 1×8+7=15).
  • hexadecimal: 0x17 = 1×16 + 7 = 23

Try it:

<script>
    var x = 0017;
    var y = 0x17;
    alert("x:" + x);
    alert("y:" + y);
</script>

Since the post parameters are converted to strings, you can put the values in quotes. Or avoid leading zeros in numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM