Web项目基础(1)

1.web项目基本结构(了解)

1.1web项目结构演示 通过tomcat运行

![image-20241119095139148](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119095139148.png

1.2web项目通过tomcat运行

tomcat需要独立启动

项目放入webapps目录中

![image-20241119094828113](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119094828113.png

1.3web项目有两种打包方式

​ war包 tomcat会自动解压缩

​ war_exploded war包解压包 不需要解压 直接使用

![image-20241119095015535](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119095015535.png

2.通过maven创建web项目

maven工具 贯穿项目生命周期的管理工具

1.所有人都使用统一的工具 对项目进行创建 编译 和打包(与开发工具无关)

2.通过统一的命令来使用 项目的创建 打包 编译 (开发 测试 部署)

3.统一的jar包管理 只需要写配置文件 从远程仓库自动下载

2.1maven安装

![image-20241119100505025](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119100505025.png

2.2配置环境变量

![image-20241119100711698](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119100711698.png

2.3maven命令测试

mvn -v 测试maven查看版本

2.4maven仓库配置

![image-20241119101530088](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119101530088.png

配置远程仓库地址

![image-20241119101700761](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119101700761.png

配置本地仓库

![image-20241119101643475](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119101643475.png

2.5idea中配置maven

![image-20241119103330505](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119103330505.png

![image-20241119103626644](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119103626644.png

![image-20241119103730915](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119103730915.png

2.6通过配置idea 创建maven项目

创建项目时 构建系统 选到maven

![image-20241119104144528](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119104144528.png

初次创建项目时 会把maven的基础依赖库(jar包) 下载到本地仓库 需要等待一会(只有第一次需要等待)

安装好后 可以使用maven相关命令 对项目操作

![image-20241119105918835](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119105918835.png

2.7mavenweb项目 及目录结构

1.添加web结构支持

![image-20241119110132842](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119110132842.png

2.勾上web应用

![image-20241119110207722](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119110207722.png

3.改目录结构

![image-20241119110404271](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119110404271.png

4.处理配置文件

![image-20241119110755239](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119110755239.png

5.可以通过maven打包web项目

![image-20241119110922683](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119110922683.png

2.8maven配置文件 POM.xml文件

配置项认知 知道有什么用就可以了

![image-20241119112823551](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119112823551.png

常用配置项:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--  pom文件标签使用的版本 自动生成   -->

<modelVersion>4.0.0</modelVersion>

<!--  当前项目的坐标
      公司名  项目名  版本号  确认到某个jar包
 -->

<groupId>com.javasm</groupId>

<artifactId>day9_mavenDemo1</artifactId>

<version>1.0</version>
<!--通过修改配置文件 让idea自动适配maven项目需要的项目打包配置
    packaging 打包方式
              jar  默认值
              war  web项目打包 要生成对应的配置文件
              pom  项目聚合使用
-->

<packaging>war</packaging>

<!-- dependencies 所有的项目依赖(jar包)配置
    查找需要jar包坐标网站
    https://mvnrepository.com/

 -->

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-api</artifactId>

<version>5.11.3</version>
<!--            jar包作用域
                默认值 compile  编译运行时使用
                      test     测试案例时使用
                      provided 编译时需要   运行时不需要
                      runtime  编译时不需要 运行时需要
    -->

<scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.36</version>

<scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->

<dependency>

<groupId>com.mysql</groupId>

<artifactId>mysql-connector-j</artifactId>

<version>8.4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>1.2.83</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>4.0.1</version>

<scope>provided</scope>
        </dependency>

    </dependencies>
<!--
项目配置参数
maven的setting.xml配置过了
-->

<properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

<!--  maven 其他插件  tomcat插件 jetty插件 内嵌运行服务器

  -->

</project>

3.通过tomcat运行web项目

tomcat是运行java代码的web应用服务器

web服务器 html css javascript

web应用服务器 html css javascript 运行程序语言代码 用代码的输出流输出页面信息

tomcat 小型免费 性能中等 可以商用 200个

jetty 框架中嵌入的小型服务器 性能较差 不能商用

jboss 中型免费 性能中等 可以商用 配置繁琐

weblogic 中大型收费 性能较好 商用 贵

部署集群

tomcat服务器详解:

1.tomcat目录结构

![image-20241119144257484](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119144257484.png

2.bin目录 启动停止脚本

![image-20241119144950590](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119144950590.png

3.conf配置

![image-20241119145531534](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119145531534.png

改端口号:

![image-20241119145601838](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119145601838.png

4其他目录 知道有什么即可 一般不会改动
5.在idea中配置tomcat插件

1.配置运行

![image-20241119150345580](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119150345580.png

2.添加tomcat运行实例

![image-20241119150521818](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119150521818.png

![image-20241119150648891](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119150648891.png

配置完可以修改参数

![image-20241119150832924](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119150832924.png

tomcat需要加载项目

![image-20241119151013678](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119151013678.png

![image-20241119151106055](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119151106055.png

控制台乱码

![image-20241119163910480](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119163910480.png

![image-20241119163945389](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119163945389.png

java.util.logging.ConsoleHandler.encoding = GBK
6.idea的tomcat插件的操作按钮

![image-20241119153024405](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119153024405.png

服务器运行控制台

![image-20241119153322469](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119153322469.png

如果修改了代码 需要重新加载代码数据

![image-20241119153746825](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119153746825.png

使用重新部署 较快 需要重新加载的数据都会加载

idea启动的tomcat 配置文件 部署目录等 都与原始的tocmat有隔离性 可以任意操作

启动时 没有root根项目

4.web项目配合java代码运行

tomcat可以运行 静态技术(html css javascript ) 和 动态技术(通过程序语言的输出流输出页面)

4.1静态技术

![image-20241119160746021](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119160746021.png

4.2动态技术

servlet

* server applet
* 服务端特定服务的处理

需要继承sun公司提供的标准类型(统一的父类(统一的接口)) HttpServlet

重写指定的方法 service(){ 当次服务处理的逻辑代码 }

配置请求路径(通过请求路径 触发当前servlet的service()方法运行)

package com.javasm;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(&quot;/person&quot;)
public class PersonServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      /*servlet
      * server applet
      * 服务端特定服务的处理
      *
      **/
       //req  请求相关的数据和参数
        //resp 响应相关的输出和对象
        System.out.println(&quot;person处理类运行了......&quot;);
        //通过url触发代码...
        MyPerson myPerson = new MyPerson();
        myPerson.readBook();

        PrintWriter writer = resp.getWriter();
        writer.write(&quot;data info&quot;);
        writer.close();

    }
}

4.3实现servlet的三种写法(了解)

HttpServlet —-> GenericServlet —-> Servlet

service() 请求进入时 会默认触发的方法

简化使用方法:

继承HttpServlet 不使用请求分发成get/post等方法 直接重写service()方法

在service()方法中写服务的处理代码

package com.javasm;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(&quot;/person&quot;)
public class PersonServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      /*servlet
      * server applet
      * 服务端特定服务的处理
      *
      **/
       //req  请求相关的数据和参数
        //resp 响应相关的输出和对象
        System.out.println(&quot;person处理类运行了......&quot;);
        //通过url触发代码...
        MyPerson myPerson = new MyPerson();
        myPerson.readBook();

        PrintWriter writer = resp.getWriter();
        writer.write(&quot;data info&quot;);
        writer.close();

    }
}

4.4servlet两种配置方式(了解)

注解

在类上直接配置请求地址

@WebServlet(&quot;/demo2&quot;)
public class ServletDemo2 extends GenericServlet {

配置文件


<servlet>
        <servlet-name>demo3</servlet-name>
        <servlet-class>com.javasm.ServletDemo3</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo3</servlet-name>
        <url-pattern>/test3</url-pattern>
    </servlet-mapping>

4.5servlet生命周期

1.服务器启动时 先准备路径和实例的对应关系(先不创建实例 懒加载)

2.当请求访问到时 先检查是否有示例对象 如果没有则创建实例 调用init方法

3.如果已经有实例 调用service()方法

4.服务器关闭时 销毁实例 调用destroy方法

5.如果配置了load-on-startup 则生命周期提前到服务器启动时创建实例

5.servlet接收请求 返回响应

![image-20241119172437058](https://www.neet0316.com/wp-content/uploads/2025/12/image-20241119172437058.png

package com.javasm;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author gfs
 * @version 0.1
 * @className LoginServlet
 * @descriptioin:
 * @date 2024/11/19 16:59
 * @since jdk11
 */
//servlet中配置的路径
// 需要补上项目名 才是完整的访问路径
// /day9/login
@WebServlet(&quot;/login&quot;)
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(&quot;login服务触发了....&quot;);
        //tomcat已经解析过请求报文
        //给tomcat指定解析请求体的字符编码
        //必须在读取第一个参数前设置 否则会失效
        req.setCharacterEncoding(&quot;utf-8&quot;);
        String username = req.getParameter(&quot;username&quot;);
        String pwd = req.getParameter(&quot;pwd&quot;);
        //tomcat 默认使用iso8859-1对字符编码
        //System.out.println( new String(username.getBytes(&quot;iso-8859-1&quot;),&quot;utf-8&quot;)  );
        System.out.println(username);
        System.out.println(pwd);

        /*
        * 查数据库 看数据对不对
        * */
        //给页面反馈
        resp.setContentType(&quot;text/html;charset=utf-8&quot;);
        PrintWriter writer = resp.getWriter();
        writer.print(username+&quot;
<h1>loginok.....<h1>");
        writer.close();

    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

<title>Title</title>
</head>
<body>
<!-- 页面中的访问路径
     相对路径
     相对根路径
     绝对路径

  -->
  <form action="/day9/login" method="post">
    <input type="text" name="username"><br>
    <input type="password" name="pwd"><br>
    <input type="submit"><br>
  </form>
</body>
</html>

servelt常见错误:

1.servlet配置地址没带/

报错信息中有这条

servlet映射中的<url pattern>[login]无效

2.servlet请求地址配置重复了

报错信息中有这条

名为 [com.javasm.ServletDemo1]和 [com.javasm.ServletDemo2] 
    的servlet不能映射为一个url模式(url-pattern) [/demo1]
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇