Servlet 就是 Sun 公司开发动态 Web 的一门技术
Sun 在这些 API (Application Programming Interface,应用程序接口)中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
1.编写一个类,实现Servlet接口。
2.把开发好的Java类部署到web服务器中。
把实现了 Servlet 接口的 Java 程序叫做,Servlet
Serlvet 接口 Sun 公司有两个默认的实现类:HttpServlet,GenericServlet
构建一个普通的Maven项目,删掉里面的src目录,以后我们的学习就在这个项目里面建立Moudel;这个空的工程就是Maven主工程(建一个WebApp Maven项目,勾选模板);
关于Maven父子工程的理解:
父项目中会有:
servlet-01
子项目中会有:
javaweb-02-maven com.maven 1.0-SNAPSHOT
父项目中的 Java 子项目可以直接使用类似于继承“is-a”
Maven 环境优化修改 web.xml 为最新的
将 maven 的结构搭建完整
建立java和resource两个目录
创建一个class类
package com.maven.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 {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter();//响应流
writer.println("Hello,Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在web.xml中加入
helloServlet com.maven.servlet.HelloServlet helloServlet /hello
实现原理
配置Tomacat,之前讲过了
对了,还有别忘记父类的pom.xml配置jar包
javax.servlet javax.servlet-api 4.0.1
之后测试localhost:8080/s1/hello
Mapping问题
1.一个 Servlet 可以指定一个映射路径
hello /hello
2.一个 Servlet 可以指定一个映射路径
hello /hello hello /hello2 hello /hello3 hello /hello4 hello /hello5
3.一个Servlet可以指定通用映射路径
hello /hello/*
4.默认请求路径
hello
/*
5.指定一些后缀或者前缀等等….
hello *.qinjiang
6.优先级问题
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;
errorServlet
com.maven.servlet.ErrorServlet
errorServlet
/*
ServletContext
Web 容器在启动的时候,它会为每个 Web 程序都创建一个对应的 ServletContext 对象,它代表了当前的 Web 应用;
共享数据在一个 servlet 中保存的数据,可以在另一个 servlet 中拿到;
设置值
package com.maven.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 {
// this.getInitParameter(); 初始化参数
// this.getServletConfig(); Servlet参数
// this.getServletContext(); Servlet上下文
ServletContext context = this.getServletContext();
String username = "小白"; //数据
//将一个数据保存在了ServletContext中,名字为:username 。值 username
context.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
传值也叫获取值
package com.maven.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 GetServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
web.xml配置
helloServlet com.maven.servlet.HelloServlet helloServlet /hello getServletContext com.maven.servlet.GetServletContext getServletContext /getC
先访问/getC
发现名字为null
在访问/hello
发现没有变化
这是因为这已经将值设置好了
再次访问/getC
发现输出“名字小白”是因为已经获取到了hello里的小白然后显示



