Idea: configure the servlet as a spring bean to inject other beans, and then use the proxy servlet to assist in configuration and operation:
I. Proxy servlet writing:
View sourceprint? 01 import javax. servlet. ServletException;
02
03 import javax. servlet. http. HttpServlet;
04
05 import javax. servlet. http. HttpServletRequest;
06
07 import javax. servlet. http. HttpServletResponse;
08
09
10
11 import org. apache. commons. logging. Log;
12
13 import org. apache. commons. logging. LogFactory;
14
15 import org. springframework. web. context. WebApplicationContext;
16
17 import org. springframework. web. context. support. WebApplicationContextUtils;
18
19
20
21 /**
22
23 * HttpServlet proxy
24
25 * @ author lwei
26
27 * @ since 2011-03-17
28
29 * @ version 1.0
30
31 */
32
33 public class HttpServletProxy extends HttpServlet {
34
35 /**
36
37 * random serialVersionUID
38
39 */
40
41 private static final long serialVersionUID =-7208519469035631940L;
42
43 Log logger = LogFactory. getLog (HttpServletProxy. class );
44
45 private String targetServlet;
46
47 private HttpServlet proxy;
48
49
50
51 public void init () throws ServletException {
52
53 this.tar getServlet = getInitParameter ("targetServlet ");
54
55 getServletBean ();
56
57 proxy. init (getServletConfig ());
58
59 logger.info (targetServlet + "was inited by HttpServletProxy successfully ......");
60
61}
62
63
64
65 private void getServletBean (){
66
67 WebApplicationContext wac =
68
69 WebApplicationContextUtils. getRequiredWebApplicationContext (getServletContext ());
70
71 this. proxy = (HttpServlet) wac. getBean (targetServlet );
72
73}
74
75
76
77 @ Override
78
79 public void service (HttpServletRequest request, HttpServletResponse response)
80
81 throws ServletException, IOException, RuntimeException {
82
83 proxy. service (request, response );
84
85}
86
87}
Ii. Business servlet writing:
View sourceprint? 01 import java. io. IOException;
02
03
04
05 import javax. servlet. ServletException;
06
07 import javax. servlet. ServletOutputStream;
08
09 import javax. servlet. http. HttpServlet;
10
11 import javax. servlet. http. HttpServletRequest;
12
13 import javax. servlet. http. HttpServletResponse;
14
15
16
17 /**
18
19 * @ author lwei
20
21 */
22
23 public class UserCheckServlet extends HttpServlet {
24
25
26
27 /**
28
29 * random serialVersionUID
30
31 */
32
33 private static final long serialVersionUID = 307563511351_22929l;
34
35
36
37 private UserService userService; (UserService is a spring-hosted bean that is injected using the set method)
38
39
40
41 // If configured directly in the spring configuration file, the set method is not required.
42
43 // how to inject beans depends on my habits of using spring
44
45 public void setUserService (UserService userService ){
46
47 this. userService = userService;
48
49}
50
51
52
53 public UserCheckServlet (){
54
55 super ();
56
57}
58
59
60
61 public void init () throws ServletException {
62
63 super. init ();
64
65}
66
67
68
69 @ Override
70
71 public void service (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
72
73 throws ServletException, IOException, RuntimeException {
74
75 ....
76
77 ....
78
79 userService. getUserByCode (); (use of bean after injection)
80
81 ....
82
83 ....
84
85}
86
87}
3. bean configured as spring in serlvet:
<Bean id = "userCheckServlet" class = "com. XXX. xxxxx. web. UserCheckServlet"/>
4. Configure the service servlet in web. xml:
View sourceprint? 01 <servlet>
02
03 <servlet-name> UserCheckProxy </servlet-name>
04
05 <servlet-class> com. XXX. xxxx. web. HttpServletProxy </servlet-class>
06
07 <init-param>
08
09 <param-name> targetServlet </param-name>
10
11 <param-value> userCheckServlet </param-value> (name of the bean configured as spring in the business servlet)
12
13 </init-param>
14
15 </servlet>
16
17 <servlet-mapping>
18
19 <servlet-name> UserCheckProxy </servlet-name>
20
21 <url-pattern>/UserCheck </url-pattern>
22
23 </servlet-mapping>
Author: "lwei's blog"