Struts的控制器原理(Servlet)

public class ControllerServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {   
        //path="XXXX.do"      
        String path = request.getServletPath();
       //去掉.do
        path = path.substring(0, path.length() - 3);
        if(path.equals("/add"))
        {   
             //Struts中会由org.apache.struts.action.ActionServlet调用Action
             //do something
        }else if(path.equals("/modify"))
        {
            //do something
        }else if(path.equals("/delete"))
        {
               //do something
        }else if(path.equals("/find"))
        {
               //do something
        }else if(path.equals("/tomodify"))
        {
             //do something
        }else if(path.equals("/login"))
        {
             //do something
        }else
        {
            throw new ServletException("unknown path:" + path);
        }
    }  
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request, response);
    }  
}
  
=====================================================
举例:
package com.allanlxf.ums.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.allanlxf.ums.entity.User;
import com.allanlxf.ums.service.UserService;

public class ControllerServlet extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
//        UserService service = null;
//        synchronized(getServletContext())
//        {
//            service = (UserService)getServletContext().getAttribute("service");
//            if(service == null)
//            {
//                service = new UserService();       
//                getServletContext().setAttribute("service", service);
//            }
//        }
       
        UserService service = (UserService)getServletContext().getAttribute("service");
       
        String path = request.getServletPath();
        path = path.substring(0, path.length() - 3);
        if(path.equals("/add"))
        {
            String name = request.getParameter("name");
            User user = new User();
            user.setName(name);
            try
            {
                service.add(user);
                forward("/find.do", request, response);
            }catch(Exception e)
            {
                e.printStackTrace();
                forward("/error.html", request, response);
            }
        }else if(path.equals("/modify"))
        {
            int id = Integer.parseInt(request.getParameter("id"));
            String name = request.getParameter("name");
            User user = new User();
            user.setId(id);
            user.setName(name);
            try
            {
                service.modify(user);
                forward("/find.do", request, response);
            }catch(Exception e)
            {
                e.printStackTrace();
                forward("/error.html", request, response);
            }
        }else if(path.equals("/delete"))
        {
            int id = Integer.parseInt(request.getParameter("id"));
            try
            {
                service.delete(id);
                forward("/find.do", request, response);
            }catch(Exception e)
            {
                e.printStackTrace();
                forward("/error.html", request, response);
            }
        }else if(path.equals("/find"))
        {
             Collection users = service.findAll();
             request.setAttribute("users", users);
             forward("/user/list", request, response);
        }else if(path.equals("/tomodify"))
        {
             int id = Integer.parseInt(request.getParameter("id"));
             User user = service.findById(id);
             request.setAttribute("user", user);
             forward("/user/modify", request, response);
        }else if(path.equals("/login"))
        {
            String userName = request.getParameter("userName");
            String password = request.getParameter("password");
            try
            {
                User user = service.login(userName, password);
                if(user != null)
                {
                    HttpSession session = request.getSession(true);
                    session.setAttribute("user", user);
                    forward("/find.do", request, response);
                }else
                {
                    forward("/login.html", request, response);
                }
            }catch(Exception e)
            {
                e.printStackTrace();
                forward("/error.html", request, response);
            }
        }else
        {
            throw new ServletException("unknown path:" + path);
        }
        System.out.println(path);
    }
   
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request, response);
    }
   
    private void forward(String path, HttpServletRequest request, HttpServletResponse response)  throws IOException, ServletException
    {
         RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
         dispatcher.forward(request, response);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值