《servlet进化史》
servlet注册方法
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/test", loadOnStartup = 1)
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
一代servlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
二代servlet(switch)
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
@WebServlet(urlPatterns = "/test", loadOnStartup = 1)
public class TestServletSwitch extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("init-service");
String operation = req.getParameter("operation");
switch (operation){
case "add":
add(req,res);
break;
case "delete":
delete(req,res);
break;
case "update":
update(req,res);
break;
case "query":
query(req,res);
break;
}
}
private void query(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("query");
}
private void update(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("update");
}
private void delete(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("delete");
}
private void add(ServletRequest req, ServletResponse res) throws IOException {
res.getWriter().write("add");
}
@Override
public void destroy() {
super.destroy();
System.out.println("init-destroy");
}
@Override
public void init() throws ServletException {
super.init();
System.out.println("init-servlet");
}
}
三代servlet(reflex)
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.lang.reflect.Method;
@WebServlet(urlPatterns = "/test2", loadOnStartup = 1)
public class TestServletReflex extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("init-service");
String operation = req.getParameter("operation");
Method[] methods = this.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(operation)) {
try {
method.invoke(this, req, res);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void query(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("query");
}
private void update(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("update");
}
private void delete(ServletRequest req, ServletResponse res) throws IOException{
res.getWriter().write("delete");
}
private void add(ServletRequest req, ServletResponse res) throws IOException {
res.getWriter().write("add");
}
@Override
public void destroy() {
super.destroy();
System.out.println("init-destroy");
}
@Override
public void init() throws ServletException {
super.init();
System.out.println("init-servlet");
}
}
四代servlet(springmvc)
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@WebServlet(value = "*.do",loadOnStartup = 1)
public class DispatchServlet extends HttpServlet {
private Map<String, Object> beanMap = new HashMap<>();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String servletPath = request.getServletPath();
servletPath = servletPath.substring(1);
int lastDotIndex = servletPath.lastIndexOf(".do");
servletPath = servletPath.substring(0, lastDotIndex);
Object controllerBeanObj = beanMap.get(servletPath);
String operate = request.getParameter("operation");
try {
Method method = controllerBeanObj.getClass().getDeclaredMethod(operate, HttpServletRequest.class, HttpServletResponse.class);
if (method != null) {
method.setAccessible(true);
method.invoke(controllerBeanObj, request, response);
} else {
throw new RuntimeException("operate值非法!");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void init() {
try {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("applicationContext.xml");
//1.创建DocumentBuilderFactory
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//2.创建DocumentBuilder对象
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
//3.创建Document对象
Document document = documentBuilder.parse(inputStream);
//4.获取所有的bean节点
NodeList beanNodeList = document.getElementsByTagName("bean");
for (int i = 0; i < beanNodeList.getLength(); i++) {
Node beanNode = beanNodeList.item(i);
if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
Element beanElement = (Element) beanNode;
String beanId = beanElement.getAttribute("id");
String className = beanElement.getAttribute("class");
Class controllerBeanClass = Class.forName(className);
Object beanObj = controllerBeanClass.newInstance();
beanMap.put(beanId, beanObj);
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<beans>
<bean id="test" class="test.TestController"/>
<bean id="test2" class="test.Test2Controller"/>
</beans>
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class TestController extends HttpServlet {
public void add(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("add");
}
public void delete(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("delete");
}
public void update(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("update");
}
public void query(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("query");
}
}
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Test2Controller extends HttpServlet {
public void add(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("add22222");
}
public void delete(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("delete2222");
}
public void update(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("update222");
}
public void query(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.getWriter().write("query2222");
}
}
- 项目结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d5868e02dd3b367e7527db95698585f5.png)