• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass javasript variable to java scriplet in side jsp?

 
Shweta Machhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a variable passord in my jsp.
I have called a javascript function on submit button, to encrypt the passord variable & agin i need to reset encrypted value to same variable.
So that in POST request decrypted passowrd wont be visible. But i cant access my form variable in my function to encrypt it.
Below is my function code snippet

function encryptData()
{
var conpassword = document.forms[0].confirmPassword.value;
alert('SCRIPT CONpass:::'+conpassword);
<%String stCon="<script>document.writeln(conpassword)</script>";
String stCon1 = new Encrypter(stCon).encrypt(); %>
document.forms[0].confirmPassword.value='<%=stCon1%>';
}

Pleae help me in this
 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,JSP from up to down the compiler,So when the page is loaded after,< % % > between has been compiled output statements.

I think you can use AJAX,DWR.
 
Shweta Machhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when i use this code & prints stCon string using out.println it gives be right value (password enetered n UI), but when i use alert for same value then it gives me value <script>document.writeln(v);</script>. why this diffrenece?
 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shweta Machhe wrote:But when i use this code & prints stCon string using out.println it gives be right value (password enetered n UI), but when i use alert for same value then it gives me value <script>document.writeln(v);</script>. why this diffrenece?

May I have a look at the complete code?

Now I guess first print right because JSP are compiled,When you go to save, JSP compiler has already been done!
You can see on the browser after the compilation of the source code,So that you may be able to see some problems!



 
Shweta Machhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have never coded in AJAX or DWR could you please guide me on this
 
Shweta Machhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the complete code , i had written it at the top of my JSP.
out.println prints jspvar=======>>>>>>Shweta on my jsp
but logger.info prints value jspvar=======>>>>>><script>document.writeln(v);</script>.

<script>
var v="Shweta";
</script>
<% String jspvar="<script>document.writeln(v);</script>";
out.println("jspvar=======>>>>>>"+jspvar);
logger.info("jspvar=======>>>>>>"+jspvar);
%>
 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this can be printed out is very normal´╝îBut when you print out later,

This code has become

And then output v,"jspvar=======>>>>>>Shweta ",

This piece of code is not exist,So you save it could not be completed!



 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
about ajax ,You can find some examples to see it,If there are not understand,Can have asked me




 
Swastik Dey
Rancher
Posts: 1686
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable contains <script>document.writeln(v)</script>. out.println renders the html content in the page. So, in your case content to be rendered is <script>document.writeln(v)</script>. Now in the rendered html page it gets interpreted as a java script code and writes the value Shweta in the page. But logger.info simply writes the value of the variable i.e <script>document.writeln(v)</script> in the log.
 
Jerome Gao
Greenhorn
Posts: 13
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very normal,<script>document.writeln(v)</script>,this is the browser analytical,Log is record variable value,Not to do processing,
 
Anuj Batra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why do you want to encrypt the password using javascript ? This is highly dangerous for the following reason

1. the algorithm will be exposed on client side for your encryption
2. the browser may have turned off the javascript so your code will not work.

I suggest you to perfor this on the server side.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!