JavaWeb

JavaWeb

Tomcat

目录结构

根目录

image-20210205193655262

conf目录

image-20210205194352840

server.xml

配置端口号:

image-20210205195919280

配置地址:

image-20210205200103306

webapps目录

网页页面文件存放目录,可以通过server.xml更改目录名称。

image-20210205214025529

每个目录对应一个app,访问localhost:8080默认进入ROOT下的index.jsp文件

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--webapps : tomcat服务器的web目录
-ROOT
-someApp : 网站app的目录名
- WEB-INF
-classes : java程序
-lib : web应用依赖的jar包
-web.xml : 网站配置文件
- index.html : 默认的首页
- static
-css
-style.css
-js
-img
- ...

MAVEN

配置

环境变量配置

MAVEN_HOME:D:\Code\Env\Java\apache-maven-3.6.3

M2_HOME:D:\Code\Env\Java\apache-maven-3.6.3\bin

PATH:%MAVEN_HOME%\bin

镜像配置

作用:加速国内下载

在maven目录下/conf/setting.xml中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

本地仓库配置

1
<localRepository>D:/Code/Env/Java/apache-maven-3.6.3/maven-repo</localRepository>

IDEA中配置

新建MAVEN项目/模块,选择相关路径

模块的maven配置文件:pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--项目依赖文件,需要其他模块手动添加到新节点中-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
</dependencies>

Servlet

  1. 编写servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.i.hello;

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

// 继承自HttpServlet
public class hello extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8"); // 设置编码
// 网页打印<h1>hello servlet!</h1>
PrintWriter writer = resp.getWriter();
writer.println("<h1>hello servlet!</h1>");
System.out.println("do Get!");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
  1. 配置路由映射

配置web.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- 定义一个servlet路由-->
<servlet>
<!-- servlet名称-->
<servlet-name>hello</servlet-name>
<!-- 对应的类-->
<servlet-class>com.i.hello.hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<!-- url映射,访问host:port/hello可以返回页面-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

Mapping

映射路径配置在对应app下web.xml下配置

需要配置servlet对应的和对应的URL

  1. 一个Servlet指定一个映射路径

1
2
3
4
5
6
7
8
9
10
<!--    定义一个servlet路由-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.i.hello.hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
  1. 一个Servlet指定多个映射路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.i.hello.hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
  1. 一个Servlet指定通用映射路径

    访问所有未显式指定的域名时都会访问/*URL映射的Servlet

    但访问/hello时会正常跳转至hello指向的内容

1
2
3
4
5
6
7
8
9
<servlet>
<servlet-name>idnex</servlet-name>
<servlet-class>com.i.hello.Index</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.i.hello.hello</servlet-class>
</servlet>

<servlet>
<servlet-name>error</servlet-name>
<servlet-class>com.i.hello.ErrorServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>error</servlet-name>
<url-pattern>/error/*</url-pattern>
</servlet-mapping>
  1. 指定url的一些后缀或前缀

1
2
3
4
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.hello</url-pattern>
</servlet-mapping>
  • 注意:*前面不能加/否则会报错。另外,但存在通配符/*时,好像会优先走通配符(未深度测试).

ServletContext

是Tomcat中的一个全局对象,是唯一的,即是个单例,所有应用共享。可以用于网页间传值。

共享数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 设置
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setHeader("content-type","text/html;charset=UTF-8");
// 获取ServletContext全局对象
ServletContext context = this.getServletContext();
// 设置属性值
context.setAttribute("name","Xiaoi");

PrintWriter writer = resp.getWriter();
writer.println("<h1>用户名已设置</h1>");
System.out.println("do Get!");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 获取
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setHeader("content-type","text/html;charset=UTF-8");

// 获取ServletContext全局对象
ServletContext context = this.getServletContext();
// 获取属性值
String contextAttribute = (String) context.getAttribute("name");

PrintWriter writer = resp.getWriter();
writer.println("<h1>用户名</h1>");
writer.println(contextAttribute);
System.out.println("do Get!");
}

JavaWeb
http://blog.rainna.xyz/2022/10/08/2022-10-08-JavaWeb/
作者
rainnalv
发布于
2022年10月8日
许可协议