TestServlet.java
----------------------------------------------------------------------
Action.java
QueryUserAction.java
UserAddAction.java
UserManager.java
Web.xml
package com.bjsxt.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String currentURI = req.getRequestURI();
System.out.println("currentURI=" + currentURI);
String path = currentURI.substring(currentURI.indexOf("/", 1));
path = path.substring(0, path.indexOf("."));
System.out.println("path=" + path);
String forwardPath = "";
if ("/user/addUser".equals(path)) {
Action action = new UserAddAction();
forwardPath = action.execute(req, res);
// System.out.println("---addUser---");
// //取得表单数据
// //调用业务逻辑
// //转向
}else if ("/user/modifyUser".equals(path)) {
System.out.println("---modifyUser---");
//取得表单数据
//调用业务逻辑
//转向
}else if ("/user/queryUser".equals(path)) {
Action action = new QueryUserAction();
forwardPath = action.execute(req, res);
// System.out.println("---queryUser---");
// //取得表单数据
// String username = req.getParameter("username");
// UserManager userManager = new UserManager();
// List userList = userManager.findUserByName(username);
// req.setAttribute("userlist", userList);
//
// //转向到c.jsp,取出list中的数据
// req.getRequestDispatcher("/a/b/c/c.jsp").forward(req, res);
}
/**
* <action-config>
* <action path="/user/addUser" class="com.bjsxt.servlet.UserAddAction" success="/add_success.jsp"/>
* <action path="/user/queryUser" class="com.bjsxt.servlet.QueryUserAction" success="/a/b/c/c.jsp"/>
* </action-config>
*
* Action action = (Action)具体Action实现
* String forwardPath = action.execute(request, response);
*
* req.getRequestDispatcher(forwardPath).forward(req, res);
*/
req.getRequestDispatcher(forwardPath).forward(req, res);
}
}
----------------------------------------------------------------------
Action.java
package com.bjsxt.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
public String execute(HttpServletRequest req, HttpServletResponse res);
}
QueryUserAction.java
package com.bjsxt.servlet;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryUserAction implements Action {
public String execute(HttpServletRequest req, HttpServletResponse res) {
System.out.println("---queryUser---");
//取得表单数据
String username = req.getParameter("username");
UserManager userManager = new UserManager();
List userList = userManager.findUserByName(username);
req.setAttribute("userlist", userList);
//转向到c.jsp,取出list中的数据
//req.getRequestDispatcher("/a/b/c/c.jsp").forward(req, res);
return "/a/b/c/c.jsp";
}
}
UserAddAction.java
package com.bjsxt.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserAddAction implements Action {
public String execute(HttpServletRequest req, HttpServletResponse res) {
System.out.println("------------------addUser--------------------");
return "/add_success.jsp";
}
}
UserManager.java
package com.bjsxt.servlet;
import java.util.ArrayList;
import java.util.List;
public class UserManager {
public List findUserByName(String name) {
List userList = new ArrayList();
userList.add("数据一");
userList.add("数据二");
return userList;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.bjsxt.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<!--
<url-pattern>/servlet/TestServlet</url-pattern>
-->
<url-pattern>*.do</url-pattern>
<!--
<url-pattern>/servlet/*</url-pattern>
-->
</servlet-mapping>
</web-app>