Build the Tomcat Servlet and Tomcat Servlet by yourself
Using a simple login instance, I will introduce how to build a servlet web program under tomcat
1. Establish the environment
Ensure that JDK and tomcat are successfully installed and environment variables are configured.
Run the java-version command to check whether JDK is successfully installed and configured. If so, the configuration is successful.
Next, check whether tomcat can be started normally.
Double-click apache-tomcat-7.0.54 \ bin \ startup. bat in the file to start the Tomcat server
Enter http: // localhost: 8080/in the browser/
2. Create the project folder test_servlet under the webapps folder under the tomcat installation directory.
And create the WEB_INF folder in this folder.
Go to the WEB-INF directory, create the classes and lib directories, and web. xml
3. Compile the LoginServlet class
Import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class LoginServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// parameter value passed through the receiving form String username = request. getParameter ("username"); String password = request. getParameter ("password"); // The console prints the output parameter values for viewing the System. out. println ("username =" + user Name); System. out. println ("password =" + password); // sets the encoding format response. setContentType ("text/html; charset = GB18030"); // outputs the browser information response. getWriter (). println ("
Compile the class and copy the generated LoginServlet. class file to the classes folder in the WEB-INF directory.
4. Describe this LoginServlet class in web. xml
<?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_3_0.xsd" version="3.0" metadata-complete="true"> <servlet><servlet-name>example</servlet-name><servlet-class>LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>example</servlet-name><url-pattern>/loginServlet</url-pattern></servlet-mapping></web-app>
Html Design
<Html>
So far, a simple servlet web program has been written. Run it below.
Start the tomcat server and enter the following URL: http: // localhost: 8080/test_servlet/login.html
Click to log on