The previous blog "Configure Tomcat environment" explains how to build the JDK and tomcat environments. This time, we write a simple servlet instance under tomcat. The example is simple: Enter the account and password on the client, after servlet processing, the user name and password are displayed on the server, and the client returns "Logon successful ".
Code html. Like HTML in. net, it interacts directly with users and can send requests to submit form data.
Logintest. Java
In my opinion, the compiled class file is the class file that is actually processed. in Tomcat deployment, the configuration file is located.
Import javax. servlet. *; import Java. io. *; import javax. servlet. HTTP. *; public class logintest extends httpservlet {protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {string username = request. getparameter ("username"); string Password = request. getparameter ("password"); system. out. println (username); system. out. println (password); response. se Theader ("Content-Type", "text/html; charset = UTF-8"); response. getwriter (). println ("welcome" + username + "use this system! ");}}
Running result
For the browser, first enter the ID: Lida, password: 123, and submit it to the servlet for processing. After servlet processing, the browser returns "Welcome lida to use this system !", As follows:
On the server side, open Tomcat and you can see that the user name and password are printed at the bottom of the command line:
Configure the following configuration file for the myfirstservlet project. This configuration file mainly describes the servlet name, the class to which the servlet is directed, and its url. Do not change the header information of the configuration file:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><servlet> <servlet-name>LoginTest</servlet-name> <servlet-class>LoginTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginTest</servlet-name> <url-pattern>/LoginTest</url-pattern> </servlet-mapping> </web-app>
The sequence diagram uses the sequence diagram of the EA to show the interaction process:
To put it simply, the processing process is:
- The user enters the account and password, and finds Tomcat through the Protocol and specified URL
- Tomcat intercepts the URL string and finds the project name, for example, myfirstservlet
- Tomcat intercepts the string again and finds the name of the servlet to be accessed. The example is logintest.
- Find the mapped address by the servlet name.
- Find the class file name by using the servlet name
- Use the above two steps to accurately locate the class file address
- Send the request to a class file, and then send the response content after processing
- The browser parses the response content and displays it to the user.
Summary
The instance is very simple, and the processing process is not much different from the general processing program under. net, because it is the first, just like the first vbprogram, simple, but also worth remembering.
For more DRP blogs, visit DRP project summary.