首页>代码>深入浅出Struts2书籍源码>/深入浅出Struts2源码/app01a/WEB-INF/src/app01a/ControllerServlet.java
package app01a;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ControllerServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException {
        process(request, response);
    }

    public void doPost(HttpServletRequest request, 
            HttpServletResponse response)
            throws IOException, ServletException {
        process(request, response);
    }

    private void process(HttpServletRequest request,
            HttpServletResponse response) 
            throws IOException, ServletException {

        String uri = request.getRequestURI();
        /*
         * uri is in the form of /contextName/resourceName, 
         * for example: /app01a/Product_input.action. 
         * However, in the case of a default context, the 
         * context name is empty, and uri has this form
         * /resourceName, e.g.: /Product_input.action
         */
        int lastIndex = uri.lastIndexOf("/");
        String action = uri.substring(lastIndex + 1); 
        // execute an action
        if (action.equals("Product_input.action")) {
            // there is nothing to be done
        } else if (action.equals("Product_save.action")) {
            // instantiate action class
            Product product = new Product();
            // populate action properties
            product.setProductName(request.getParameter("productName"));
            product.setDescription(request.getParameter("description"));
            product.setPrice(request.getParameter("price"));
            // execute action method
            product.save();
            // store action in a scope variable for the view
            request.setAttribute("product", product);
        }

        // forward to a view
        String dispatchUrl = null;
        if (action.equals("Product_input.action")) {
            dispatchUrl = "/jsp/ProductForm.jsp";
        } else if (action.equals("Product_save.action")) {
            dispatchUrl = "/jsp/ProductDetails.jsp";
        }
        if (dispatchUrl != null) {
            RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl);
            rd.forward(request, response);
        }
    }
}
最近下载更多
songweifly  LV1 2019年6月16日
朱云飞  LV13 2018年10月20日
zz12534  LV12 2018年7月6日
z924931408  LV25 2018年5月23日
676323640  LV4 2017年8月29日
knife037  LV1 2017年2月14日
dhddxp19780109-1  LV1 2016年10月17日
1044775980  LV1 2016年10月1日
穆尼尔丁  LV12 2016年5月26日
wujixing909  LV2 2016年2月18日
最近浏览更多
你脚上银铃响了  LV1 2021年10月27日
liuhongbing123  LV5 2020年7月13日
lsqhlj  LV1 2020年6月15日
songweifly  LV1 2019年6月16日
花花yayayaya  LV2 2019年6月13日
15101081698  LV3 2019年3月10日
789aaa  LV2 2019年1月2日
pengboooo  LV12 2018年12月22日
SDGWGs  LV1 2018年11月14日
zhaolihuiziyu  LV1 2018年10月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友