Two fatal disadvantages of Servlet (interview questions):
- There are many configuration items in web.xml file: due to the increase of Servlet, a large number of Servlet configurations will be generated
- Servlet has container dependency reason: HttpServletRequest and HttpServletResponse objects are created by the Web container when a servlet processes a get or post request
Implementation steps of ActionServlet:
1. Create Action interface:
public interface Action { public String execute(HttpServletRequest request,HttpServletResponse response); }
2. Create an Action to override the Action interface:
public class HelloWorld implements Action{ @Override public String execute(HttpServletRequest request, HttpServletResponse response) { System.out.println("action Access succeeded!"); //Prompt the processing method of Action to be executed return "index.jsp"; //Return the Action address to be redirected } }
3. Create ActionFilter (a method to implement Filter interface rewriting Filter life cycle):
public class ActionFilter implements Filter{
private HttpServletRequest request = null; //Create an http request object. Private httpservletresponse = null; / / create an http response object
@Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//Get HTTP request and response objects this.request = (HttpServletRequest)request; this.response = (HttpServletResponse)response;
/*
Get the uri of the action to be accessed through HttpServletRequest
ServletRequest must be converted to HttpServletRequest to get URI
*/ String uri = this.request.getRequestURI();
String actionName = ActionUtil.getActionName(uri);//Get the name of the Action by parsing the uri string with getActionName() try { Action action = (Action)Class.forName("th.action."+actionName).newInstance(); //Create Action object through reflection mechanism String page = action.execute(this.request, this.response); //Discard the Action processing method and return the request and response back this.request.getRequestDispatcher(page).forward(this.request, this.response); //Redirect according to the jump page string returned by Action } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void destroy() { // TODO Auto-generated method stub } }
getAction() method to resolve URI:
public class ActionUtil { public static String getActionName(String uri){ String[] str = uri.split("/"); String result = str[str.length-1].substring(0, str[str.length-1].indexOf(".")); return result; } }
4. To configure the Filter filter in Web.xml:
<filter> <filter-name>ActionFilter</filter-name> <filter-class>th.filter.ActionFilter</filter-class> </filter> <filter-mapping> <filter-name>ActionFilter</filter-name> < URL pattern > *. Action < / url pattern > <! -- distinguish whether to access action or servlet according to the address with suffix ending with *. Action -- > </filter-mapping>
5. Create a test jsp page:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <a href="HelloWorld.action" >Request one action</a> //To distinguish whether a servlet or an action is accessed, use the href="*.action" </body> </html>