Java Reference
In-Depth Information
LISTING 21.1
Continued
28: }
29:
30: public void doGet(HttpServletRequest req, HttpServletResponse res)
31: throws ServletException, IOException {
32:
33: doPost(req, res);
34: }
35:
36: String translate(String input) {
37: StringBuffer output = new StringBuffer();
38: if (input != null) {
39: for (int i = 0; i < input.length(); i++) {
40: char inChar = input.charAt(i);
41: if ((inChar >= 'A') & (inChar <= 'Z')) {
42: inChar += 13;
43: if (inChar > 'Z')
44: inChar -= 26;
45: }
46: if ((inChar >= 'a') & (inChar <= 'z')) {
47: inChar += 13;
48: if (inChar > 'z')
49: inChar -= 26;
50: }
51: output.append(inChar);
52: }
53: }
54: return output.toString();
55: }
56: }
After saving the servlet, compile it with the Java compiler.
The Rot13 servlet receives text from a web form, translates it using ROT-13, and then
displays the result in a new web form. ROT-13 is a trivial method of encrypting text
through letter substitution. Each letter of the alphabet is replaced with the letter that's 13
places away: A becomes N, N becomes A, B becomes O, O becomes B, C becomes P, P
becomes C, and so on.
Because the ROT-13 encryption scheme is easy to decode, it isn't used when information
must remain secret. Instead, it's used casually on Internet discussion forums such as
Usenet newsgroups. For example, if someone on a movie newsgroup wants to share a
spoiler that reveals a plot detail about an upcoming movie, she can encode it in ROT-13
to prevent people from reading it accidentally.
21
 
Search WWH ::




Custom Search