我构建我的 servlet 了吗?
Have i built my servlet right?
我开发了一个 Web 应用程序 (WebApplication 1),它有 1 个 servlet 和许多 JSP,并且这个应用程序具有一定的功能(可以说是 No1)。
在该应用程序中,在 servlet 的 processRequest(HttpServletRequest request, HttpServletResponse response)
方法中,有许多 if else
语句以检测用户想要做什么。
Servlet
public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
init();
HttpSession session = request.getSession(true);
String jspAction = request.getParameter("action");
if(jspAction.equals("home")){
//do sth
RequestDispatcher disp = getServletContext().getRequestDispatcher("/home.jsp");
disp.forward(request, response);
}else if(jspAction.equals("out")){
//do sth
RequestDispatcher disp = getServletContext().getRequestDispatcher("/out.jsp");
disp.forward(request, response);
}//etc..
}
}
在 JSP links 或表单提交中有这种 link 格式 MyServlet?action=out
。按照这种方法,我此时有 25 个 if 语句。
所以我的问题是:这是构建我的应用程序的正确方法还是我应该为我想执行的每个功能创建不同的 servlet?
您可以使用 java.util.HashMap<String, String>
来配置您的导航规则。 Map key为action参数值,Map value为jsp页
通过这种方式您可以执行以下操作
HashMap<String, String> navigationMap = new HashMap<String, String>();
navigationMap.put("home", "/home.jsp");
navigationMap.put("out", "/out.jsp");
...
String jspAction = request.getParameter("action");
String page = navigationMap.get(jspAction);
RequestDispatcher disp = getServletContext().getRequestDispatcher(page);
...
这会解决你的大 if-else 问题,但如果你想为每个 navigationRule 做一些动作,你将不得不使用更复杂的 Map 来映射处理请求和 return 页。
这就是像 Structs 或 JSF 这样的 MVC 框架所做的某种方式,你为什么不试试 MVC 框架呢?
虽然这是一个有效(且非常基本)的解决方案,但最好为此目的使用 MVC 框架,而不是重新发明轮子。作为应用程序访问点的中央 servlet 是一种常见的做法,例如可以在早期 Java EE design patterns 中找到,如 "front controller" 和 "dispatcher view".
你应该看看 Spring MVC,或者 Struts 2,例如。
我开发了一个 Web 应用程序 (WebApplication 1),它有 1 个 servlet 和许多 JSP,并且这个应用程序具有一定的功能(可以说是 No1)。
在该应用程序中,在 servlet 的 processRequest(HttpServletRequest request, HttpServletResponse response)
方法中,有许多 if else
语句以检测用户想要做什么。
Servlet
public class MyServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
init();
HttpSession session = request.getSession(true);
String jspAction = request.getParameter("action");
if(jspAction.equals("home")){
//do sth
RequestDispatcher disp = getServletContext().getRequestDispatcher("/home.jsp");
disp.forward(request, response);
}else if(jspAction.equals("out")){
//do sth
RequestDispatcher disp = getServletContext().getRequestDispatcher("/out.jsp");
disp.forward(request, response);
}//etc..
}
}
在 JSP links 或表单提交中有这种 link 格式 MyServlet?action=out
。按照这种方法,我此时有 25 个 if 语句。
所以我的问题是:这是构建我的应用程序的正确方法还是我应该为我想执行的每个功能创建不同的 servlet?
您可以使用 java.util.HashMap<String, String>
来配置您的导航规则。 Map key为action参数值,Map value为jsp页
通过这种方式您可以执行以下操作
HashMap<String, String> navigationMap = new HashMap<String, String>();
navigationMap.put("home", "/home.jsp");
navigationMap.put("out", "/out.jsp");
...
String jspAction = request.getParameter("action");
String page = navigationMap.get(jspAction);
RequestDispatcher disp = getServletContext().getRequestDispatcher(page);
...
这会解决你的大 if-else 问题,但如果你想为每个 navigationRule 做一些动作,你将不得不使用更复杂的 Map 来映射处理请求和 return 页。
这就是像 Structs 或 JSF 这样的 MVC 框架所做的某种方式,你为什么不试试 MVC 框架呢?
虽然这是一个有效(且非常基本)的解决方案,但最好为此目的使用 MVC 框架,而不是重新发明轮子。作为应用程序访问点的中央 servlet 是一种常见的做法,例如可以在早期 Java EE design patterns 中找到,如 "front controller" 和 "dispatcher view".
你应该看看 Spring MVC,或者 Struts 2,例如。