Java Tutorial - Java Script : Developing Servlets

Java Tutorial - Java Script :
Developing Servlets
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.


To make the ROT-13 servlet available, you must publish its class files in a folder on your web server that has been designated for Java servlets. Tomcat is organized so that servlets, other classes, and JSP pages are placed in subfolders of the software’s webapps folder. One way to deploy a servlet’s class file is to store it in a WEB-INF\classes subfolder somewhere in the webapps hierarchy of folders. If you chose to install them during installation, Tomcat 5.5 includes several sample servlets in the servlets-examples and jsp-examples folders inside webapps. You can deploy the ROT-13 servlet in the servlet-examples folder by storing Rot13.class in webapps\servlet-examples\WEB-INF\classes (Windows) or webapps/servletexamples/ WEB-INF/classes (Linux). If you place the Rot13.class file in this folder, edit the web.xml file in its parent folder and add the following lines:

<servlet>
<servlet-name>Rot13</servlet-name>
<servlet-class>Rot13</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rot13</servlet-name>
<url-pattern>/servlet/Rot13</url-pattern>
</servlet-mapping>

The web.xml file configures a web application such as the group of servlet examples stored in wepapps\servlet-examples. These lines must be placed somewhere after the opening <web-app> tag and before the closing </web-app> tag. After adding the class file and editing web.xml, restart Tomcat and run the servlet by loading its address with a web browser. The address of the servlet depends on where it was stored in the webapps folder. If you used the preceding configuration, it’s in /servlets-examples/servlet/Rot13, as in http://localhost:8080 /servlets-examples/servlet/Rot13.