根据上一篇,我们学会了基本的Servlet的实现,这篇从Servlet实现类的演变为切入点学习
Servlet
这是所有Servlet都需要是去实现的接口,一切一切的祖宗;
public class MyServlet2 implements Servlet {
//init方法,在Servlet 创建完成后调用
//我们第一次请求时,Servlet创建
//我们可以设置Servlet的创建时机
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2222222 init 调用了");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
GenericServlet
已经是实现了Servlet接口,经过底层代码,通过继承GenericServlet类让我们直接可以将最重要的service直接拿出来使用,不会像之前要重写一堆方法;
public class ServletDemo extends GenericServlet {
//对外提供的方法
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service 方法调用了");
//使用配置对象,获取初始化参数的值
ServletConfig servletConfig = this.getServletConfig();
String pwd = servletConfig.getInitParameter("pwd");
ServletConfig servletConfig1 = super.getServletConfig();
String pwd1 = servletConfig.getInitParameter("pwd");
System.out.println(pwd);
System.out.println(pwd1);
String pwd2 = this.getInitParameter("pwd");
System.out.println(pwd2);
}
}
HttpServlet(最常用)
HttpServlet首先必须读取Http请求的内容。Servlet容器负责创建HttpServlet对象,并把Http请求直接封装到HttpServlet对象中,大大简化了HttpServlet解析请求数据的工作量。
主要重写doget和dopost方法
//定义Sevlet的方式3 开发常用的方式
public class ServletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理get请求
System.out.println("get 请求收到了");
//获取用户提交的用户名和密码 使用JDBC 去查询数据库
//把结果返回
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理post请求
System.out.println("post 请求收到了");
this.doGet(req,resp);
}
}
IDEA修改Servlet模板
修改完之后我们就可以直接建立servlet
Servlet注解配置
在我们建立Servlet后,如果勾选了注解,那么我们可以通过注解进行配置
@WebServlet(name = "MyServlet",value = "/demo")
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
ServletContext 接口
ServletContext 接口 :他是一个全局域对象:代表整个web应用,由服务器来创建,你只是获取使用
服务器一开启,就会创建ServletContext对象
然后我们获取整个对象来使用
怎么获取?
我们可以通过配置对象来获取
ServletConfig servletConfig = this.getServletConfig();
ServletContext servletContext = servletConfig.getServletContext();
public class ServletDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("收到请求了");
//ServletContext: 接口:他是一个全局域对象:代表整个web应用,由服务器来创建,你只是获取使用
//服务器一开启,就会创建ServletContext对象
//然后我们获取整个对象来使用
//怎么获取?
//我们可以通过配置对象来获取
ServletConfig servletConfig = this.getServletConfig();
ServletContext servletContext = servletConfig.getServletContext();
System.out.println(servletContext);
//域:范围: ServletContext 全局域对象,代表整个web应用,域对象 ,可以在他代表的范围内来共享数据
// ServletContext 单例的
int num = 100;
//把这个数据存到域对象里面,在其他地方,从域中取出来用
servletContext.setAttribute("mydata", num); //往域中存数据
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
作为域对象:有三个方法
域对象有四个
ServletContext 全局域对象,代表整个web应用
Session 会话域 范围一次会话
Request 请求域 范围一次请求
PageContext 页面域 代表一个jsp页面
servletContext.setAttribute();
servletContext.getAttribute();
servletContext.removeAttribute();
动态获取服务器路径
web 下 有一个 a.txt文件
web/WEB-INF 有一个b.txt 文件
src 下有一个 c.txt 文件
我们要读取这三个不同地方的文件 怎么读?
FileInputStream in = new FileInputStream("E:\\IdeaProjects\\20190903-ServletDemo-上午2\\out\\artifacts\\20190903_ServletDemo_2_war_exploded\\WEB-INF\\classes\\c.txt");
System.out.println(in);
是找不到的;
因为现在的是web项目,这些文件都会进入服务器的地址,因此我们要动态获取服务器的路径
使用String realPath = context.getRealPath("/");
@WebServlet(name = "ServletTest")
public class ServletTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求来了");
//获取全局域对象
ServletContext context = this.getServletContext();
//FileInputStream in = new FileInputStream("E:\\IdeaProjects\\20190903-ServletDemo-上午2\\out\\artifacts\\20190903_ServletDemo_2_war_exploded\\WEB-INF\\classes\\c.txt");
// System.out.println(in);
//我们要动态获取服务器的路径
String realPath = context.getRealPath("/");
System.out.println(realPath);
// D:\apache - tomcat - 8.5 .42\webapps\myweb\
//读取c.txt
FileInputStream in = new FileInputStream(realPath + "/WEB-INF/classes/c.txt");
//读取b.txt
FileInputStream in2 = new FileInputStream(realPath + "/WEB-INF/b.txt");
//读取 a.txt
FileInputStream in3 = new FileInputStream(realPath + "/a.txt");
System.out.println(in);
System.out.println(in2);
System.out.println(in3);
ServletContext servletContext = this.getServletConfig().getServletContext();
servletContext.setAttribute("username","zhangsan");
servletContext.getAttribute("username");
servletContext.
removeAttribute("username");
//使用普通工具类
MyUtils.demo();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
public class MyUtils {
public static void demo(){
try {
FileInputStream fileInputStream = new FileInputStream("src/c.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}