1、创建一个类
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class DeptController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html><body>");
out.println("<h1>hello dept</h1>");
out.println("<body></html>");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ResourceBundle;
/*所有的servlet都通过这个AllController来转发*/
public class AllController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如何转发给具体的干活的Controller
//System.out.println(req.getRequestURL());//结果:http://localhost:8080/mvc01_war_exploded/dept.do
//System.out.println(req.getRequestURI());//结果:/mvc01_war_exploded/dept.do
//System.out.println(req.getServletPath());//结果:/dept.do
//获得请求的url
String requestUrl = req.getServletPath().substring(1);
//判断请求地址来进行响应的调用(推荐使用工厂模式)
if ("dept.do".equals(requestUrl)) {
DeptController dept = new DeptController();
dept.doPost(req,resp);
}
}
}
点击查看代码
<?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,现在都使用一个用*.do来标识-->
<servlet>
<servlet-name>all</servlet-name>
<servlet-class>com.bh.controller.AllController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>all</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--<servlet>
<servlet-name>dept</servlet-name>
<servlet-class>com.bh.controller.DeptController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dept</servlet-name>
<url-pattern>/dept</url-pattern>
</servlet-mapping>-->
</web-app>
工厂模式版
1、创建一个接口
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface IAction {
public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class DeptController1 implements IAction {
@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>hello DEPT</h1>");
out.println("</body></html>");
}
}
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class EmpController implements IAction{
@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>hello EMP</h1>");
out.println("</body></html>");
}
}
点击查看代码
dept.do=com.bh.controller.DeptController1
emp.do=com.bh.controller.EmpController
点击查看代码
package com.bh.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ResourceBundle;
/*所有的servlet都通过这个AllController来转发*/
public class AllController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如何转发给具体的干活的Controller
//System.out.println(req.getRequestURL());//结果:http://localhost:8080/mvc01_war_exploded/dept.do
//System.out.println(req.getRequestURI());//结果:/mvc01_war_exploded/dept.do
//System.out.println(req.getServletPath());//结果:/dept.do
//获得请求的url
String requestUrl = req.getServletPath().substring(1);
//判断请求地址来进行响应的调用(推荐使用工厂模式)
/* if ("dept.do".equals(requestUrl)) {
DeptController dept = new DeptController();
dept.doPost(req,resp);
} else if("emp.do".equals(requestUrl)){
EmpController emp = new EmpController();
} else if ("xxx.do") {
XXXControll xxx = new XXControl();
}*/
//工厂模式
//读取url的配置文件
ResourceBundle rb = ResourceBundle.getBundle("url");
String classname = rb.getString(requestUrl);
IAction action=null;
//根据干活的类,创建类的实例
try {
Class clz = Class.forName(classname);
action = (IAction) clz.newInstance();
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
action.doAction(req,resp);
}
}
点击查看代码
<?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>all</servlet-name>
<servlet-class>com.bh.controller.AllController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>all</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>