adong搬砖

导航

tomcat和servlet简单demo

tomcat

结构

image

 

javac -encoding utf-8 -cp lib/servlet-api.jar tcServletTest.java

将java文件编译成class,将java文件和外部jar包路径(相对或者绝对都行)融合到编译后的class文件中

 -encoding utf-8:解决中文编译乱码;

image

 

web.xml基本配置

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

</web-app>

 

tomcat如果需要识别class里面注解,需要metadata-complete改为false

image

 

servlet基本用法(必须在java容器中,没有main方法)

image

 

image

 

posted on 2026-01-21 18:04  adong搬砖  阅读(2)  评论(0)    收藏  举报