我正在使用页面控制器模式。如何通过检测请求操作然后根据结果进行调度,将同一个控制器用于两个不同的页面?
这是我的代码:
帐户.jsp
<form name="input" action="<%=request.getContextPath() %>/edit" method="get">
<input type="submit" value="Modifier" />
</form>
帐户 Servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("received HTTP GET");
String action = request.getParameter("action");
if (action == null)
{
// the account page
dispatch(request, response, "/account");
}
else if (action == "/edit") {
// the popup edit page
dispatch(request, response, "/edit");
}
protected void dispatch(HttpServletRequest request,
HttpServletResponse response, String page)
throws javax.servlet.ServletException, java.io.IOException {
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(page);
dispatcher.forward(request, response);
}
}