package com.sdnu.javaweb;import jakarta.servlet.*;import java.io.IOException;/*** 实现servlet接口** @author Beyong* @date 2023/03/03 20:44**/
public class AServlet implements Servlet {public AServlet() {System.out.println("A的无参构造方法");}@Overridepublic void init(ServletConfig servletConfig) throws ServletException {}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}
package com.sdnu.javaweb;import jakarta.servlet.*;import java.io.IOException;/*** 实现Servlet** @author Beyong* @date 2023/03/03 20:49**/
public class BServlet implements Servlet {public BServlet() {System.out.println("B的无参构造方法");}@Overridepublic void init(ServletConfig servletConfig) throws ServletException {}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}
aServlet com.sdnu.javaweb.AServlet aServlet /a bServlet com.sdnu.javaweb.BServlet bServlet /b
在控制台发现没有执行构造方法,说明服务器启动的时候,构造方法没有执行。
数字
数字越小,则优先级越高。
package com.sdnu.javaweb;import jakarta.servlet.*;import java.io.IOException;/*** 实现servlet接口** @author Beyong* @date 2023/03/03 20:44**/
public class AServlet implements Servlet {public AServlet() {System.out.println("A的无参构造方法");}@Overridepublic void init(ServletConfig servletConfig) throws ServletException {System.out.println("AServlet'init method execute");}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("AServlet'service method execute");}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {System.out.println("AServlet'destroy method execute ");}
}
结论:
控制台输出:
AServlet’service method execute
后面的请求,Servlet对象并没有创建,而是使用之前的创建好的Servlet对象,调用该对象的方法。
servlet对象是单例的,但是servlet类不符合单例模式(假单例)。
无参构造方法只执行一次,init方法也只执行一次,即第一次。
只要用户发送请求,service必然执行。
服务器在关闭前,执行servlet对象的destroy方法,destroy方法执行结束后,AServlet对象的才会被销毁,AServlet对象内存才会释放。
接口
package com.sdnu.javaweb.adapter;/*** 接口* @author Beyong* @date 2023/03/04 11:08**/
public interface MyInterface {void m1();void m2();void m3();void m4();void m5();void m6();void m7();void core();
}
UserService:
package com.sdnu.javaweb.adapter;/*** 用户** @author Beyong* @date 2023/03/04 11:10**/
public class UserService implements MyInterface{@Overridepublic void m1() {}@Overridepublic void m2() {}@Overridepublic void m3() {}@Overridepublic void m4() {}@Overridepublic void m5() {}@Overridepublic void m6() {}@Overridepublic void m7() {}@Overridepublic void core() {System.out.println("UserService'core method execute!!!");}
}
CustomerService:
package com.sdnu.javaweb.adapter;/*** CustomerService** @author Beyong* @date 2023/03/04 11:13**/
public class CustomerService implements MyInterface{@Overridepublic void m1() {}@Overridepublic void m2() {System.out.println("Customer'm2 method execute");}@Overridepublic void m3() {}@Overridepublic void m4() {}@Overridepublic void m5() {}@Overridepublic void m6() {}@Overridepublic void m7() {}@Overridepublic void core() {}
}
不使用适配器模式下,则代码每一个service都要取实现接口。
接口:
package com.sdnu.javaweb.adapter2;/*** 接口* @author Beyong* @date 2023/03/04 11:24**/
public interface MyInterface {void m1();void m2();void m3();void m4();void m5();void m6();void m7();void core();
}
用户适配器:
package com.sdnu.javaweb.adapter2;/*** UserService适配器** @author Beyong* @date 2023/03/04 11:26**/
public abstract class UserAdapter implements MyInterface{@Overridepublic void m1() {}@Overridepublic void m2() {}@Overridepublic void m3() {}@Overridepublic void m4() {}@Overridepublic void m5() {}@Overridepublic void m6() {}@Overridepublic void m7() {}public abstract void core();
}
顾客适配器:
package com.sdnu.javaweb.adapter2;/*** 客户适配器** @author Beyong* @date 2023/03/04 11:28**/
public abstract class CustomerAdapter implements MyInterface{public abstract void m1();@Overridepublic void m2() {}@Overridepublic void m3() {}@Overridepublic void m4() {}@Overridepublic void m5() {}@Overridepublic void m6() {}@Overridepublic void m7() {}@Overridepublic void core() {}
}
用户:
package com.sdnu.javaweb.adapter2;/*** 普通用户** @author Beyong* @date 2023/03/04 11:34**/
public class UserService extends UserAdapter{@Overridepublic void core() {}
}
顾客:
package com.sdnu.javaweb.adapter2;/*** 顾客** @author Beyong* @date 2023/03/04 11:35**/
public class CustomerService extends CustomerAdapter{@Overridepublic void m1() {}
}
我们写一个通用的GenericServlet
package com.sdnu.javaweb.servlet;import jakarta.servlet.*;import java.io.IOException;/*** servlet适配器** @author Beyong* @date 2023/03/04 11:48**/
public abstract class GenericServlet implements Servlet {@Overridepublic void init(ServletConfig servletConfig) throws ServletException {}@Overridepublic ServletConfig getServletConfig() {return null;}public abstract void service(ServletRequest servletRequest, ServletResponse servletResponse);@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}
LoginServlet
package com.sdnu.javaweb.servlet;import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;/*** c的servlet** @author Beyong* @date 2023/03/04 11:49**/
public class LoginServlet extends GenericServlet{@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) {System.out.println("c'service method execute");}
}
web.xml
loginServlet com.sdnu.javaweb.servlet.LoginServlet loginServlet /login
地址:http://127.0.0.1:8080/lifecycle/login
进一步改造:
package com.sdnu.javaweb.servlet;import jakarta.servlet.*;import java.io.IOException;/*** servlet适配器** @author Beyong* @date 2023/03/04 11:48**/
public abstract class GenericServlet implements Servlet {private ServletConfig config;@Overridepublic void init(ServletConfig config) throws ServletException {this.config = config;this.init();}/*** 这个init方法供子类重写*/public void init(){}@Overridepublic ServletConfig getServletConfig() {return null;}public abstract void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException;@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}
标签中的配置信息自动包装到ServletConfig对象中去。
package com.sdnu.javaweb.servlet;import jakarta.servlet.*;
import sun.plugin2.util.ParameterNames;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;/*** servletConfig测试2** @author Beyong* @date 2023/03/06 13:05**/
public class ConfigServletTest2 extends GenericServlet {@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {servletResponse.setContentType("text/html");PrintWriter out = servletResponse.getWriter();ServletConfig config = this.getServletConfig();out.print("servletConfig对象是:" + config.toString());out.print("");String servletName = config.getServletName();out.print("" + servletName + " ");out.print("");Enumeration initParameterNames = config.getInitParameterNames();while(initParameterNames.hasMoreElements()){String parameterName = initParameterNames.nextElement();String parameterVal = config.getInitParameter(parameterName);out.print(parameterName + "=" + parameterVal);out.print("
");}String driver = config.getInitParameter("driver");out.print(driver);// Enumeration names = this.getInitParameterNames();
// while(names.hasMoreElements()){
// String name = names.nextElement();
// String value = this.getInitParameter(name);
// out.print(name + " = " + value);
// out.print("
");
// }}
}
configTest com.sdnu.javaweb.servlet.ConfigServletTest configTest /test configTest2 com.sdnu.javaweb.servlet.ConfigServletTest2 driver com.mysql.cj.jdbc.Driver url jdbc:mysql://localhost:3306/sdnu user root password root123 configTest2 /test2
Enumeration initParameterNames = application.getInitParameterNames();
String value = application.getInitParameter(name);
以上两个方法获取context-param标签中的name和value
startIndex 2
(2获取应用的根路径
String contextPath = application.getContextPath();
(3)获取文件的绝对路径
public String getRealPath(String path)
(4)访问日志
public void log(String message);
public void log(String message, Throwable t);
Tomcat服务器的Log目录下都有哪些日志信息?
catalina.2023-02-28.log 服务器的java程序运行的控制信息
localhost.2023-03-07.log ServletContext对象的log方法记录的日志存储到这个文件中
localhost_access_log.2023-02-28.txt 访问日志
存:public void setAttribute(String name, Object value);
取:public void getAttribute(String name);
删:public void removeAttribute(String name);
User user = new User("jack", "123456");
application.setAttribute("UserObj", user);
Object userObj = application.getAttribute("UserObj");
out.print(userObj);
out.print("");
jakarta.servlet.Servlet 【接口】
jakarta.servlet.GenericServlet implements Servlet 【抽象类】
jakarta.servlet.http.HttpServlet extends GenericServlet 【类】
以后编写程序继承HttpServlet
作者:Beyong
出处:Beyong博客
github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。