As the development of web service (WS), applications based on web services (WS), which are convent and platform-independent, have become increasingly popular in recent years. However, how to identify, generate and compose services has become an open issue recently. This paper proposes a method based on program slicing to realize the generation and composition of web services. This paper introduces the method about how to generate a WSDL file and a SOAP message from source codes as well as the theory of function dependence graph (FDG). In addition, this paper gives the way to generate a proxy service for each service, which allows users to easily call a service. The results of experiments show that our generation and composition methods of WS are feasible and flexible.