如何在java中屏蔽值?
我有一个pinNumber值。如何屏蔽它(即)说如果我的pinNumber是1234,并用四个星号符号屏蔽它,而不是显示数字。此屏蔽值将使用jsp显示在网页中。如何在java中屏蔽值?,java,masking,Java,Masking,我有一个pinNumber值。如何屏蔽它(即)说如果我的pinNumber是1234,并用四个星号符号屏蔽它,而不是显示数字。此屏蔽值将使用jsp显示在网页中。 <input type="PASSWORD" name="password"> 如果要在标签上显示密码,只需显示5或6个星号字符,因为您不想通过将该标签的长度与密码的长度组合在一起来提供线索。 如果您要在标签上显示密码,只需显示5或6个星号字符,因为您不想通过将该标签的长度与密码的长度结合起来来给他们提供线索。在代码中
<input type="PASSWORD" name="password">
如果要在标签上显示密码,只需显示5或6个星号字符,因为您不想通过将该标签的长度与密码的长度组合在一起来提供线索。
如果您要在标签上显示密码,只需显示5或6个星号字符,因为您不想通过将该标签的长度与密码的长度结合起来来给他们提供线索。在代码中的某一点上,您会将数字转换为字符串以供显示。 此时,您只需调用String的replaceAll方法,将0-9中的所有字符替换为*字符:
s.replaceAll("[0-9]", "*")
这就是直接在Java中实现的方法。在JSP中,如其他海报所示,如果您在输入中选择“密码”类型,它将为您处理。在代码中的某个点,您将把数字转换为字符串以供显示。 此时,您只需调用String的replaceAll方法,将0-9中的所有字符替换为*字符:
s.replaceAll("[0-9]", "*")
这就是直接在Java中实现的方法。在JSP中,如其他海报所示,如果您在输入中选择“密码”类型,它将为您处理。您创建这样的表单
<form action="something" method="post">
pinNumber:<input type="password" name="pass"/>
<input type="submit" value="OK"/>
</form>
品号:
然后它会将您创建的表单更改为*
<form action="something" method="post">
pinNumber:<input type="password" name="pass"/>
<input type="submit" value="OK"/>
</form>
品号:
然后它会将更改为*,这段代码可能会对您有所帮助
class Password {
final String password; // the string to mask
Password(String password) { this.password = password; } // needs null protection
// allow this to be equal to any string
// reconsider this approach if adding it to a map or something?
public boolean equals(Object o) {
return password.equals(o);
}
// we don't need anything special that the string doesnt
public int hashCode() { return password.hashCode(); }
// send stars if anyone asks to see the string - consider sending just
// "******" instead of the length, that way you don't reveal the password's length
// which might be protected information
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; < password.length(); i++)
sb.append("*");
return sb.toString();
}
}
类密码{
最终字符串密码;//要屏蔽的字符串
密码(字符串密码){this.Password=Password;}//需要空保护
//允许该值等于任何字符串
//如果将其添加到地图或其他东西中,请重新考虑此方法?
公共布尔等于(对象o){
返回密码。等于(o);
}
//我们不需要任何字符串不需要的特殊内容
public int hashCode(){return password.hashCode();}
如果有人要求看字符串,就发送星星。
//“******”而不是长度,这样您就不会显示密码的长度
//哪些可能是受保护的信息
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
对于(int i=0;
此代码可能会对您有所帮助
class Password {
final String password; // the string to mask
Password(String password) { this.password = password; } // needs null protection
// allow this to be equal to any string
// reconsider this approach if adding it to a map or something?
public boolean equals(Object o) {
return password.equals(o);
}
// we don't need anything special that the string doesnt
public int hashCode() { return password.hashCode(); }
// send stars if anyone asks to see the string - consider sending just
// "******" instead of the length, that way you don't reveal the password's length
// which might be protected information
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; < password.length(); i++)
sb.append("*");
return sb.toString();
}
}
类密码{
最终字符串密码;//要屏蔽的字符串
密码(字符串密码){this.Password=Password;}//需要空保护
//允许该值等于任何字符串
//如果将其添加到地图或其他东西中,请重新考虑此方法?
公共布尔等于(对象o){
返回密码。等于(o);
}
//我们不需要任何字符串不需要的特殊内容
public int hashCode(){return password.hashCode();}
如果有人要求看字符串,就发送星星。
//“******”而不是长度,这样您就不会显示密码的长度
//哪些可能是受保护的信息
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
对于(int i=0;
我在我的许多应用程序中使用了以下代码,它运行得很好
public class Test {
public static void main(String[] args) {
String number = "1234";
StringBuffer maskValue = new StringBuffer();
if(number != null && number.length() >0){
for (int i = 0; i < number.length(); i++) {
maskValue.append("*");
}
}
System.out.println("Masked Value of 1234 is "+maskValue);
}
}
Ans - Masked Value of 1234 is ****
公共类测试{
公共静态void main(字符串[]args){
字符串编号=“1234”;
StringBuffer maskValue=新的StringBuffer();
if(number!=null&&number.length()>0){
对于(int i=0;i
我在我的许多应用程序中使用了以下代码,它运行得很好
public class Test {
public static void main(String[] args) {
String number = "1234";
StringBuffer maskValue = new StringBuffer();
if(number != null && number.length() >0){
for (int i = 0; i < number.length(); i++) {
maskValue.append("*");
}
}
System.out.println("Masked Value of 1234 is "+maskValue);
}
}
Ans - Masked Value of 1234 is ****
公共类测试{
公共静态void main(字符串[]args){
字符串编号=“1234”;
StringBuffer maskValue=新的StringBuffer();
if(number!=null&&number.length()>0){
对于(int i=0;i
两种方式(这将屏蔽所有字符,而不仅仅是数字):
我使用第二种方法有两种(这将屏蔽所有字符,而不仅仅是数字):
我可能会使用第二个?您将如何在jsp中显示您的密码?为什么要在java类中屏蔽密码,而不是在屏幕上显示密码?可能吧?您将如何在jsp中显示密码?为什么要在java类中屏蔽密码,而不是在屏幕上显示密码?