学习javaweb4

学习javaweb

GenericServlet抽象类 


    前面我们编写Servlet一直是通过实现Servlet接口来编写的,但是,使用这种方法,则必须要实现Servlet接口中定义的所有的方法,即使有一些方法中没有任何东西也要去实现,并且还需要自己手动的维护ServletConfig这个对象的引用。因此,这样去实现Servlet是比较麻烦的。

  1. void init(ServletConfig var1) throws ServletException;

    幸好,GenericServlet抽象类的出现很好的解决了这个问题。本着尽可能使代码简洁的原则,GenericServlet实现了Servlet和ServletConfig接口,下面是GenericServlet抽象类的具体代码:

 

  1. public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
  2. private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
  3. private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
  4. private transient ServletConfig config;
  5. public GenericServlet() {
  6. }
  7. public void destroy() {
  8. }
  9. public String getInitParameter(String name) {
  10. ServletConfig sc = this.getServletConfig();
  11. if (sc == null) {
  12. throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
  13. } else {
  14. return sc.getInitParameter(name);
  15. }
  16. }
  17. public Enumeration<String> getInitParameterNames() {
  18. ServletConfig sc = this.getServletConfig();
  19. if (sc == null) {
  20. throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
  21. } else {
  22. return sc.getInitParameterNames();
  23. }
  24. }
  25. public ServletConfig getServletConfig() {
  26. return this.config;
  27. }
  28. public ServletContext getServletContext() {
  29. ServletConfig sc = this.getServletConfig();
  30. if (sc == null) {
  31. throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
  32. } else {
  33. return sc.getServletContext();
  34. }
  35. }
  36. public String getServletInfo() {
  37. return "";
  38. }
  39. public void init(ServletConfig config) throws ServletException {
  40. this.config = config;
  41. this.init();
  42. }
  43. public void init() throws ServletException {
  44. }
  45. public void log(String msg) {
  46. this.getServletContext().log(this.getServletName() + ": " + msg);
  47. }
  48. public void log(String message, Throwable t) {
  49. this.getServletContext().log(this.getServletName() + ": " + message, t);
  50. }
  51. public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
  52. public String getServletName() {
  53. ServletConfig sc = this.getServletConfig();
  54. if (sc == null) {
  55. throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
  56. } else {
  57. return sc.getServletName();
  58. }
  59. }
  60. }
  1.     其中,GenericServlet抽象类相比于直接实现Servlet接口,有以下几个好处:

 

1.为Servlet接口中的所有方法提供了默认的实现,则程序员需要什么就直接改什么,不再需要把所有的方法都自己实现了。

2.提供方法,包围ServletConfig对象中的方法。

 

posted @   青空zsl  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示