/**
- 1、后台的代码首先要放在src下
- 2、后台的代码不能再放在main下面,这个是jvm运行默认的地方
- 3、现在代码交给了tomcat,tomcat要求不让放在main下面
- 4、要求如下:
-
1、一个类如果想让tomcat帮着管理,需要继承HttpServlet这个类
-
缺包:统一放在web\WEB-INF/lib目录下,没有就创建一个
-
2、重写里面的doGet、doPost方法
*/
这个文件就像aa.txt一样,是需要被加载到内存的
tomcat会来读取这个文件,它要求这个xml有书写规范,我们按它的要求来就好了
流程:
tomcat会先走web.xml
然后找servlet-mapping的url-pattern
二级目录非注解 和限制于一个demo中 还有一个是全局的 web.xml
<servlet>
<servlet-name>aaa</servlet-name><!-- 3、通过servlet-mapping的名字找到了这里 -->
<servlet-class>Demo04</servlet-class><!-- 4、找到对应的 类 -->
</servlet>
<servlet-mapping>
<servlet-name>aaa</servlet-name><!-- 2、找到这个servlet-name -->
<url-pattern>/hh</url-pattern><!--1、浏览器的访问路径-->
</servlet-mapping>
<servlet>
<servlet-name>bbb</servlet-name><!-- 3、通过servlet-mapping的名字找到了这里 -->
<servlet-class>Demo05_initParam</servlet-class><!-- 4、找到对应的 类 -->
<!-- 只在当前servlet中有效 -->
<init-param>
<param-name>filename</param-name>
<param-value>jdbc.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>bbb</servlet-name><!-- 2、找到这个servlet-name -->
<url-pattern>/pd</url-pattern><!--1、浏览器的访问路径-->
</servlet-mapping>
<!--全局的-->
<context-param>
<param-name>sex</param-name>
<param-value>nv</param-value>
</context-param>
限定在于一个demo中的
-
alt+insert 或 ctrl+o 快捷键 生成 get和post
-
1、每个servlet都会提供一个请求,它们都有自己的路径,他们会实现不同的需求
-
2、Servlet现在被tomcat管理
-
3、tomcat允许每个Servlet配置初始化参数
-
ServletConfig类
-
比如我们可以利这个初始化参数,传个文件名,然后在java中加载文件等各种操作
-
注:
-
修改web.xml文件 或 新增Servlet文件都需要重启tomcat
public class Demo05_initParam extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("--------测试 init-param ------------");
String str = this.getServletConfig().getInitParameter("filename");
System.out.println("初始化参数为:"+str);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
全局资源
- ServletContext类
- 叫上下文 承上启下
- 该类中存储的变量,可以全局使用
- 如果有些数据,是希望所有serlvet共用,就可以将数据存在该类中
第一步 在一个demo中设置好 全局资源
@WebServlet("/qian")
public class Demo06 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//从配置文件的 <context-param>拿数据
String sex = this.getServletContext().getInitParameter("sex");
System.out.println("全局变量:"+sex);
System.out.println("------在java中向ServletContext类中存数据-------------");
this.getServletContext().setAttribute("gender","nv");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
第二步 去另一个demo中去调用
System.out.println("--------测试Demo06(qian)在ServletContext中存的全局变量gender-------------");
System.out.println(this.getServletContext().getAttribute("gender"));
-
前端get方式发起的请求会进入doGet方法中
-
前端post方式发起的请求会进入doPost方法中
-
我们可以在doPost中调用doGet
-
这个方法带了两个参数,
-
req:request(请求),它就是前端发过来的http协议包,被tomcat解析成HttpServletRequest对象了
-
tomcat9中:
-
get方式传中文没有乱码
-
post方式有乱码
设置 防止乱码 vm-options设置为
-Dfile.encoding=UTF-8
public class Demo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo04....");
System.out.println("前端传来了:"+req.getParameter("username"));
System.out.println("哈哈");
System.out.println("--------测试 init-param ------------");
String str = this.getServletConfig().getInitParameter("filename");
System.out.println("初始化参数为:"+str);
System.out.println("--------测试Demo06(qian)在ServletContext中存的全局变量gender-------------");
System.out.println(this.getServletContext().getAttribute("gender"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
- 1、后台的代码首先要放在src下
- 2、后台的代码不能再放在main下面,这个是jvm运行默认的地方
- 3、现在代码交给了tomcat,tomcat要求不让放在main下面
- 4、要求如下:
-
1、一个类如果想让tomcat帮着管理,需要继承HttpServlet这个类
-
缺包:
-
1、统一放在web\WEB-INF/lib目录下,没有就创建一个
-
2、可以单独导tomcat相关依赖
-
2、重写里面的doGet、doPost方法
-
alt+insert键 、ctrl+o
-
3、在Demo02上添加一个注解
-
@WebServlet("/nn") 二级目录 在浏览器输入栏中 http://localhost:8080/webD01_war_exploded/ 后面加二级目录 就可以去到 加注解的demo中
- http://localhost:8080/webD01_war_exploded/ 这个你自己写的项目路径
- 二级路径 就是具体 展示 执行的demo
-
其中"/nn"在浏览器中的访问路径
-
当我们输入localhost:8080/web01/nn,
-
tomcat会去找 类上面有@WebServlet注解的,并且里面的值是"/nn"的
-
找到了这个类,就可以找到doGet方法
-
就可以执行66666了
@WebServlet("/nn")
public class Demo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("66666");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
获取 项目路径 和绝对路径
System.out.println("获取项目名:"+this.getServletContext().getContextPath());
System.out.println("获取绝对路径:"+this.getServletContext().getRealPath("/"));