其实就是把昨天的这个程序放到一个WEB应用程序了,大体的程序没有什么变化,只有少许改动。创建一个FencePassword类,这个类有两个属性,一个是oldMessage表示加密前的信息,一个是newMessage表示加密后的信息。在password.jsp页面输入你要加密的信息,然后就在同一个页面得到加密后的信息。其中,FencePassword类的encrypt方法来加密,clearAll方法来清除页面上的所有信息。
用JSF做的,需要一些必要的包,放在WEB-INF/lib下。基本配置可以参照相关的JSF文章。
首先是web.xml:
<?
xml version="1.0" encoding="UTF-8"?><
web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet
</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping><display-name>MyJSF</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</
web-app>然后是FencePassword类:
package
src;public
class FencePassword {String
oldMessage;String
newMessage; public String encrypt() { int l = 0;StringBuffer str1 =
new StringBuffer();StringBuffer str2 =
new StringBuffer();StringBuffer str =
new StringBuffer();String temp;
StringBuffer sb =
new StringBuffer();String[] s =
oldMessage.split(" "); for (int a = 0; a < s.length; a++) {sb.append(s[a]);
}
temp = sb.toString();
char[] c = temp.toCharArray();l = temp.length();
for (int i = 0; i < l;) {str1.append(c[i++]);
if (i == l && l % 2 != 0) break; elsestr2.append(c[i++]);
}
str.append(str1);
str.append(str2);
newMessage = str.toString(); return "result";}
public void clearAll() { oldMessage = ""; newMessage = "";}
public String getNewMessage() { return newMessage;}
public void setNewMessage(String newMessage) { this.newMessage = newMessage;}
public String getOldMessage() { return oldMessage;}
public void setOldMessage(String oldMessage) { this.oldMessage = oldMessage;}
}
接着是password.jsp页面:
<%@
page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><%@
taglib uri="http://java.sun.com/jsf/core" prefix="f" %><%@
taglib uri="http://java.sun.com/jsf/html" prefix="h" %><!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><
html><
head><
meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><
title>Get Fence Password</title></
head><
body><
f:view> <h:form>Input your message:
<h:inputText value="#{fence.oldMessage}" /> <h:commandButton value="Encrypt" action="#{fence.encrypt}" /> <h:commandButton value="Clear" action="#{fence.clearAll}" /> <br/>Encrypted message:
<h:outputText value="#{fence.newMessage}" /> </h:form></
f:view></
body></
html>最后就是JSF的配置文件faces-config.xml:
<?
xml version="1.0" encoding="UTF-8"?><!
DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"><
faces-config> <navigation-rule> <navigation-case> <from-outcome>result</from-outcome> <to-view-id>/password.jsp</to-view-id> </navigation-case> </navigation-rule> <managed-bean> <managed-bean-name>fence</managed-bean-name> <managed-bean-class>src.FencePassword</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean></
faces-config>在FencePassword类中,加密完成后通过返回result找到password.jsp(其实就是同一个页面)。bean的名字取为fence。首先在输入框中输入信息,点击Encrypt后得到加密的信息,点击Clear后清除所有信息。