Build the Tomcat Servlet and Tomcat Servlet by yourself

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.