JavaWeb-基础
Tomcat
目录结构
|--bin 脚本目录
|--conf 配置
|--lib 依赖的jar包
|--logs 日志
|--temp 临时文件
|--webapps 存放应用程序
|--work 工作目录
\conf\server.xml
配置
配置默认端口号
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
配置host
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
web目录结构
|--webapps
|--root
|--test
|--index.html 目录首页
|--WEB-INF
|--classes java程序
|--lib web所依赖的jar包
|--web.xml 网站配置文件
Maven
在Java web开发中,需要使用大量的jar包,Maven能够自动导入和配置jar包。
Maven项目构架管理工具 规定了目录配置 核心:约定大于配置
配置
配置环境变量
配置镜像
maven下的conf\settings.xml
在
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
配置本地仓库
<localRepository>E:\java\apache-maven-3.8.1\local-repo</localRepository>
配置IDEA
再重新创建项目就是一个干净的maven项目了
配置tomcat
运行程序
Servlet
- Servlet就是sun公司开发动态web的一门技术
- Sun在这些API中提供一个接口叫做: Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤;
- 编写一个类,实现Servlet接口
- 把开发好的Java类部署到web服务器中。
- 把实现了Servlet接口的Java程序叫做Servlet
创建servlet工程
配置pom.xml
<dependencies>
<!-- 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>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
当前工程就是Maven的主工程
新建Module
优化Maven
配置最新的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
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"
metadata-complete="true">
</web-app>
搭建完整结构
HelloServlet
-
编写一个普通类
-
实现Servlet接口,继承自HttpServlet
package com.th0r.servlet; 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; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.println("Hello Servlet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
-
编写Servlet的映射
JAVA程序使用浏览器访问,需要在web服务中注册Servlet,还需要给出访问路径
<!-- 注册Servlet--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.th0r.servlet.HelloServlet</servlet-class> </servlet> <!-- Servlet请求的路径--> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
-
配置tomcat
在添加Artfact时 如果没有显示Application context 重新打开IDEA
-
运行
原理
借用网上的一张图
Mapping
-
一个Servlet可以指定一个映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
-
一个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>
-
一个Servlet可以指定通用映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping>
-
默认请求路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
-
指定后缀或前缀
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/*.qingjiag</url-pattern> </servlet-mapping>
*前面不能加项目映射路径
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求
ServletContext
共享数据
ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。
HelloServlet
package com.th0r.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String uname = "root";
context.setAttribute("uname", uname);
System.out.printf("hello");
}
}
GetServlet
package com.th0r.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String uname = (String)context.getAttribute("uname");
resp.getWriter().println("GetServlet: " + uname);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
获取初始化参数
web.xml
<context-param>
<param-name>db</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>db</servlet-name>
<servlet-class>com.th0r.servlet.Demo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>db</servlet-name>
<url-pattern>/db</url-pattern>
</servlet-mapping>
demo1.java
package com.th0r.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String db = context.getInitParameter("db");
resp.getWriter().println(db);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
请求转发
web.xml
<servlet>
<servlet-name>rd</servlet-name>
<servlet-class>com.th0r.servlet.Demo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>rd</servlet-name>
<url-pattern>/rd</url-pattern>
</servlet-mapping>
Demo2
package com.th0r.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
RequestDispatcher reqDis = context.getRequestDispatcher("/db");
reqDis.forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
读取文件资源
Properties
- java目录下的.peoperties/.xml
- recourse目录下的.properties/.xml
都打包在了同一个路径下:classes 俗称 classpath
Demo3
package com.th0r.servlet;
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.InputStream;
import java.util.Properties;
public class Demo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/prop.properties");
Properties prop = new Properties();
prop.load(is);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().println(username + ": " + password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}