Servlet笔记6--Servlet程序改进

Posted 拉夫德尔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet笔记6--Servlet程序改进相关的知识,希望对你有一定的参考价值。

第一步改进,GenericServlet:

  我们目前所有放入Servlet类直接实现了javax.servlet.Servlet接口,但是这个接口中有很多方法是目前不需要的,我们可能只需要编写service。直接实现Servlet接口代码丑陋,有必要在中间添加一个适配器,以后所有的Servlet类不再直接实现Servlet接口,应该去继承适配器类。

  适配器除了可以让代码优雅之外,我们还可以在适配器中提供大量的方法,子类继承之后,可以在子类中直接使用,方便编程。

  自己使用缺省适配器设计模式实现的GenericServlet类:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Servlet;
 6 import javax.servlet.ServletConfig;
 7 import javax.servlet.ServletContext;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 
12 /*
13  * GenericSerrvlet是一个适配器,这个适配器是一个Servlet
14  * 以后javaweb程序员无需直接实现Servlet接口,去继承这个适配器即可,重写service方法。
15  *  
16  * @author Administrator
17  */
18 
19 public abstract class GenericServlet implements Servlet {
20 
21     ServletConfig config;
22     
23     //该方法不能被覆盖,因为被覆盖之后,config就获取不到了,所以方法前面加了final
24     public final void init(ServletConfig config) throws ServletException {
25         this.config = config;
26         this.init();
27     }
28     
29     /*
30      * 若在初始化时刻需要执行一段特殊的代码,建议重写无参数的init方法,注意动态绑定,会让上面的init方法调用子类重写的init方法
31      */
32     public void init(){
33         
34     }
35     
36     public abstract void service(ServletRequest request, ServletResponse response) 
37             throws ServletException, IOException;
38     
39     public void destroy() {
40 
41     }
42 
43     public ServletConfig getServletConfig() {
44         return config;
45     }
46 
47     public String getServletInfo() {
48         return "qjj";
49     }
50 
51     //---------------------以下所有的方法都是拓展方法,方便子类的使用------------------------------
52     
53     public ServletContext getServletContext(){
54         return getServletConfig().getServletContext();
55     }
56 }

  其实sun公司已经为我们实现了这个适配器类,可以直接调用:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletRequest;
 8 import javax.servlet.ServletResponse;
 9 
10 //public class HelloServlet extends GenericServlet {
11 public class HelloServlet extends javax.servlet.GenericServlet {    
12     //sun公司已经为我们写了适配器,并且里面有很多其他的方法
13     
14     private static final long serialVersionUID = 1L;
15 
16     @Override
17     public void init() {
18         System.out.println("HelloServlet\'s init......");
19     }
20 
21     @Override
22     public void service(ServletRequest request, ServletResponse response)
23             throws ServletException, IOException {
24         response.setContentType("text/html;charset=UTF-8");
25         PrintWriter out = response.getWriter();
26         out.print("hello world!");
27         out.print("<br>");
28         out.print(getServletContext());
29     }
30 
31 }

 

HTTP协议相关:

  前端页面中的GET请求和POST请求代码:

 1  <html>
 2      <head>
 3          <title>login page</title>
 4          <meta name="keywords" content="keyword1,keyword2,keyword3">
 5         <meta name="description" content="this is my page">
 6         <meta name="content-type" content="text/html; charset=UTF-8">
 7      </head>
 8      <body>
 9      
10          GET方式
11          <form action="/prj-servlet-09/system/login">
12              username<input type="text" name="username"><br>
13              password<input type="password" name="password"><br>
14              <input type="submit" value="login">
15          </form>
16          <hr>
17          GET方式
18          <form action="/prj-servlet-09/system/login" method="get">
19              username<input type="text" name="username"><br>
20              password<input type="password" name="password"><br>
21              <input type="submit" value="login">
22          </form>
23          <hr>
24          POST方式
25          <form action="/prj-servlet-09/system/login" method="post">
26              username<input type="text" name="username"><br>
27              password<input type="password" name="password"><br>
28              <input type="submit" value="login">
29          </form>
30      </body>
31  
32  </html>

 

第二步改进,HTTPServlet:

  具体代码实现:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.GenericServlet;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 public class LoginServlet extends GenericServlet {
14 
15     private static final long serialVersionUID = 1L;
16 
17     @Override
18     public void service(ServletRequest req, ServletResponse res)
19             throws ServletException, IOException {
20         //将ServletRequest,ServletResponse强制类型转换成带有Http的接口类型
21         HttpServletRequest request = (HttpServletRequest)req;
22         HttpServletResponse response = (HttpServletResponse)res;
23         
24         response.setContentType("text/html;charset=UTF-8");
25         PrintWriter out = response.getWriter();
26         
27         //获取浏览器发送的请求方式
28         String method = request.getMethod();
29         //LoginServlet是处理登录的,要求前端必须发送POST请求
30         if("GET".equals(method)){
31             //前端报错
32             out.print("405-您应当发送POST请求");
33             //后端报错
34             throw new RuntimeException("405-您应当发送POST请求");
35         }
36         
37         //若程序执行到这里证明用户发送到的请求是POST请求,程序应当正常执行
38         out.print("正在登录...");
39     }
40 
41 }

  解决方法:自己用模板方法设计模式设计HTTPServlet:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.GenericServlet;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 public class HttpServlet extends GenericServlet {
14 
15     private static final long serialVersionUID = 1L;
16 
17     @Override
18     public void service(ServletRequest req, ServletResponse res)
19             throws ServletException, IOException {
20         HttpServletRequest request = (HttpServletRequest)req;
21         HttpServletResponse response = (HttpServletResponse)res;
22         service(request,response);
23     }
24     
25     public void service(HttpServletRequest request, HttpServletResponse response)
26             throws ServletException, IOException {
27         String method = request.getMethod();
28         if("GET".equals(method)){
29             doGet(request,response);
30         }else if("POST".equals(method)){
31             doPost(request,response);
32         } 
33     }
34 
35     public void doGet(HttpServletRequest request, HttpServletResponse response) 
36             throws ServletException, IOException {
37         response.setContentType("text/html;charset=UTF-8");
38         PrintWriter out = response.getWriter();
39         out.print("405-您应当发送POST请求");
40         throw new RuntimeException("405-您应当发送POST请求");
41     }
42     
43     public void doPost(HttpServletRequest request, HttpServletResponse response)
44             throws ServletException, IOException {
45         response.setContentType("text/html;charset=UTF-8");
46         PrintWriter out = response.getWriter();
47         out.print("405-您应当发送GET请求");
48         throw new RuntimeException("405-您应当发送GET请求");
49     }
50 
51 }

  接下写自己的Servlet时,要继承自HttpServlet,为get请求则重写doGet方法,为post请求则重写doPost方法。

  其实sun公司已经为我们实现了这个HttpServlet类,可以直接调用:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 //public class LoginServlet extends HttpServlet {
10 public class LoginServlet extends javax.servlet.http.HttpServlet {
11 
12     private static final long serialVersionUID = 1L;
13 
14     @Override
15     public void doPost(HttpServletRequest request, HttpServletResponse response)
16             throws ServletException, IOException {
17         response.getWriter().print("login......");
18     }
19 }

  至此,以后写自己的Servlet时,都要继承自HttpServlet,并且使用doPost或者doGet方法。

以上是关于Servlet笔记6--Servlet程序改进的主要内容,如果未能解决你的问题,请参考以下文章

Servlet&HTTP&Request笔记

6.Servlet

6.servlet和jsp的区别

Servlet详解

Servlet编程

servlet